import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.*;
/**
* <p>Title: 文件過濾器演示</p>
* <p>Description: FileChooserDemo文件使用的文件過濾器</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: MyFilter.java</p>
* @version 1.0
*/public class MyFilter extends FileFilter {
private String files;
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}String extension = getExtension(f);
if (extension != null) {
if (extension.equals("java")) {//定義過濾Java文件
return true;
} else {
return false;
}}
return false;
}//過濾器描述
public String getDescription() {
return "Java";
}
/**
*<br>方法說明:獲取文件擴展名
*<br>輸入參數:
*<br>返回類型:
*/
public static String getExtension(File f) {
String ext = null;
String s = f.getName();
int i = s.lastIndexOf(´.´);if (i > 0 && i < s.length() - 1) {
ext = s.substring(i+1).toLowerCase();
}
return ext;
}
}import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
/**
* <p>Title: 文件對話框演示</p>
* <p>Description: 演示打開文件對話框和保存文件對話框,使用了文件過濾。</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: FileChooserDemo.java</p>
* @version 1.0
*/public class FileChooserDemo extends JPanel
implements ActionListener {
static private final String newline = "\n";
JButton openButton, saveButton;
JTextArea log;
JFileChooser fc;public FileChooserDemo() {
super(new BorderLayout());log = new JTextArea(15,40);
log.setMargin(new Insets(10,10,10,10));
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);//創建一個文件過濾,使用當前目錄
fc = new JFileChooser(".");
//過濾條件在MyFilter類中定義
fc.addChoosableFileFilter(new MyFilter());openButton = new JButton("打開文件",
createImageIcon("images/Open16.gif"));
openButton.addActionListener(this);saveButton = new JButton("保存文件",
createImageIcon("images/Save16.gif"));
saveButton.addActionListener(this);//構建一個面板,添加“打開文件”和“保存文件”
JPanel buttonPanel = new JPanel();
buttonPanel.add(openButton);
buttonPanel.add(saveButton);add(buttonPanel, BorderLayout.PAGE_START);
add(logScrollPane, BorderLayout.CENTER);
}
/**
*<br>方法說明:事件處理
*<br>輸入參數:
*<br>返回類型:
*/
public void actionPerformed(ActionEvent e) {//當點擊“打開文件”按鈕
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//在這裡添加一些對文件的處理
log.append("打開文件: " + file.getName() + newline);
} else {
log.append("打開文件被用戶取消!" + newline);
}//點擊“保存文件”按鈕
} else if (e.getSource() == saveButton) {
int returnVal = fc.showSaveDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//在這裡添加一些對文件的處理
log.append("保存文件: " + file.getName() + newline);
} else {
log.append("保存文件被用戶取消!" + newline);
}
}
}
/**
*<br>方法說明:獲取圖像對象
*<br>輸入參數:String path 圖片路徑
*<br>返回類型:ImageIcon 圖片對象
*/
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = FileChooserDemo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn´t find file: " + path);
return null;
}
}public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);//創建窗體
JFrame frame = new JFrame("FileChooserDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//創建一個面板
JComponent newContentPane = new FileChooserDemo();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);//顯示窗體
frame.pack();
frame.setVisible(true);
}
}