JAVA中的文件選擇控件

在在java中也有着和vc中一樣的文件選擇控件,那就是JFileChooser,該控件可以實現和window下的文件選擇控件同樣的功能,具體的用法如下:

JFileChooser chooser = new JFileChooser();

   // Note: source for ExampleFileFilter can be found in FileChooserDemo,

   // under the demo/jfc directory in the JDK.

//過濾掉文件類型

FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");

   chooser.setFileFilter(filter);

//顯示文件選擇對話框,其中parent爲父窗口,一般爲JFrame

 

   int returnVal = chooser.showOpenDialog(parent);

   if(returnVal == JFileChooser.APPROVE_OPTION) {

      System.out.println("You chose to open this file: " +

           chooser.getSelectedFile().getAbsolutePath());    //得到文件的絕對路徑

   }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章