JAVA----记录点滴

 

(一)图形界面基础

 

 1 import javax.swing.*;
 2 import java.awt.*;
 3 
 4 public class MainFrame extends JFrame{
 5     
 6     //容器
 7     private Container con = getContentPane();
 8     
 9     public static void main(String[] args) {
10         
11         new MainFrame();
12         
13     }
14     
15     public MainFrame(){
16         
17         this.setLayout(new BorderLayout());
18         
19         Toolkit tk = Toolkit.getDefaultToolkit();
20         Dimension dim = tk.getScreenSize();
21         
22         int screenWidth = (int)dim.getWidth();
23         int screenHeight = (int)dim.getHeight();
24         
25         //获取屏幕的边界
26         Insets screenInsets = tk.getScreenInsets(this.getGraphicsConfiguration());
27         //获取底部任务栏高度
28         int taskBarHeight = screenInsets.bottom;
29         
30         //this.setIconImage(new ImageIcon("a.gif"));
31         
32         this.setTitle("Crasher");
33         this.setSize(screenWidth, screenHeight-taskBarHeight);
34         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
35         this.setVisible(true);
36         
37         
38     }
39 }

 

(二)java实现目录选择的几种方法

 

方法一,使用JFileChooser控件:

示例:

JFileChooser chooser = new JFileChooser();

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//设置只能选择目录
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
  selectPath =chooser.getSelectedFile().getPath() ;

  System.out.println ( "你选择的目录是:" + selectPath );
  chooser.hide();
}

 

方法二,使用DirectoryDialog控件:

示例:

Display display = new Display ();
Shell shell = new Shell (display);

 DirectoryDialog dialog = new DirectoryDialog (shell);
 selectPath = dialog.open() ;
 System.out.println ( "你选择的目录是:" + selectPath );
 while (!shell.isDisposed()) {
        if (!display.readAndDispatch ()) display.sleep ();      

 }
 display.dispose ();

 

(三)JAVA 保留二位小数

 1 方式一:
 2 
 3 四舍五入
 4 double   f   =   111231.5585;
 5 BigDecimal   b   =   new   BigDecimal(f);
 6 double   f1   =   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
 7 保留两位小数
 8 
 9 方式二:
10 
11 java.text.DecimalFormat   df   =new   java.text.DecimalFormat(”#.00″);
12 df.format(你要格式化的数字);
13 
14 例:new java.text.DecimalFormat(”#.00″).format(3.1415926)
15 
16 #.00 表示两位小数 #.0000四位小数 以此类推…
17 
18 方式三:
19 
20 double d = 3.1415926;
21 
22 String result = String .format(”%.2f”);
23 
24 %.2f %. 表示 小数点前任意位数   2 表示两位小数 格式后的结果为f 表示浮点型。
25 
26 方式四:
27 
28 此外如果使用struts标签做输出的话,有个format属性,设置为format="0.00"就是保留两位小数
29 
30 例如:<bean:write name="entity" property="dkhAFSumPl"  format="0.00" />

 

 

 

 

 

 

 

 

 

 

 

posted @ 2012-12-25 18:20  china_victory  阅读(275)  评论(0编辑  收藏  举报