<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
1
2 //
3 // @name JFileChooserDemo.java
4 //
5 // @discription 文件选择器演示程序
6 //
7 // @author hcm
8 //
9 // @date 2006-12
10 //
11
12
13 import java.awt. * ;
14 import java.awt.event. * ;
15 import javax.swing. * ;
16 import java.io. * ;
17
18 // 文件选择器演示
19
20 public class JFileChooserDemo extends JFrame {
21 private JFileChooser chooser; // 文件选择器
22 private JButton button; // 选择文件按钮
23 private JComboBox comboBox; // 用于设定文件对话框作用(打开还是保存文件)
24
25 public JFileChooserDemo() {
26 super ( " JFileChooser 演示 " ); // 调用父类构造函数
27 Container contentPane = getContentPane(); // 得到容器
28 contentPane.setLayout( new FlowLayout()); // 设置布局管理器为Flowlayout
29 chooser = new JFileChooser(); // 初始化文件选择器
30 button = new JButton( " 选择文件 " ); // 初始化按钮
31 comboBox = new JComboBox(); // 初始化组合框
32 comboBox.addItem( " 打开 " ); // 增加组合框列表内容
33 comboBox.addItem( " 保存 " );
34 contentPane.add(comboBox); // 增加组件到容器
35 contentPane.add(button);
36
37 button.addActionListener( new ActionListener() { // 按钮事件处理
38 public void actionPerformed(ActionEvent e) {
39 int state; // 文件选择器返回状态
40 // chooser.removeChoosableFileFilter(chooser.getAcceptAllFileFilter()); // 移去所有文件过滤器
41 // chooser.addChoosableFileFilter(new MyFileFilter("gif","图像文件")); // 增加文件过滤器,接爱gif文件
42
43 if (comboBox.getSelectedIndex() == 0 ) // 组合框为"打开"
44
45 {
46 System.out.println( " ~~~~~~~~~~~~~ " + 0 );
47 state = chooser.showOpenDialog( null ); // 显示打开文件对话框
48 System.out.println( " zhuangtai==== " + state);
49 } else {
50 System.out.println( " ~~~~~~~~~~~~~ " + 1 );
51 state = chooser.showSaveDialog( null ); // 显示保存文件对话框
52 }
53
54 File file = chooser.getSelectedFile(); // 得到选择的文件
55 if ( true == ( file != null ) ) {
56 System.out.println(file.toString());
57 }
58 if (file != null && state == JFileChooser.APPROVE_OPTION) { // 选择了文件并点击了打开可保存按钮
59 JOptionPane.showMessageDialog( null , file.getPath()); // 显示提示信息
60 } else if (state == JFileChooser.CANCEL_OPTION) { // 点击了撤销按钮
61 JOptionPane.showMessageDialog( null , " 退出! " ); // 显示提示信息
62 } else if (state == JFileChooser.ERROR_OPTION) {
63 JOptionPane.showMessageDialog( null , " 错误! " ); // 显示提示信息
64 }
65 }
66 });
67
68 this .setSize( 200 , 100 ); // 设置窗口大小
69 this .setVisible( true ); // 设置窗口可见
70 this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭窗口时退出程序
71 }
72
73 public static void main(String args[]) {
74 new JFileChooserDemo();
75 }
76 }
77
2 //
3 // @name JFileChooserDemo.java
4 //
5 // @discription 文件选择器演示程序
6 //
7 // @author hcm
8 //
9 // @date 2006-12
10 //
11
12
13 import java.awt. * ;
14 import java.awt.event. * ;
15 import javax.swing. * ;
16 import java.io. * ;
17
18 // 文件选择器演示
19
20 public class JFileChooserDemo extends JFrame {
21 private JFileChooser chooser; // 文件选择器
22 private JButton button; // 选择文件按钮
23 private JComboBox comboBox; // 用于设定文件对话框作用(打开还是保存文件)
24
25 public JFileChooserDemo() {
26 super ( " JFileChooser 演示 " ); // 调用父类构造函数
27 Container contentPane = getContentPane(); // 得到容器
28 contentPane.setLayout( new FlowLayout()); // 设置布局管理器为Flowlayout
29 chooser = new JFileChooser(); // 初始化文件选择器
30 button = new JButton( " 选择文件 " ); // 初始化按钮
31 comboBox = new JComboBox(); // 初始化组合框
32 comboBox.addItem( " 打开 " ); // 增加组合框列表内容
33 comboBox.addItem( " 保存 " );
34 contentPane.add(comboBox); // 增加组件到容器
35 contentPane.add(button);
36
37 button.addActionListener( new ActionListener() { // 按钮事件处理
38 public void actionPerformed(ActionEvent e) {
39 int state; // 文件选择器返回状态
40 // chooser.removeChoosableFileFilter(chooser.getAcceptAllFileFilter()); // 移去所有文件过滤器
41 // chooser.addChoosableFileFilter(new MyFileFilter("gif","图像文件")); // 增加文件过滤器,接爱gif文件
42
43 if (comboBox.getSelectedIndex() == 0 ) // 组合框为"打开"
44
45 {
46 System.out.println( " ~~~~~~~~~~~~~ " + 0 );
47 state = chooser.showOpenDialog( null ); // 显示打开文件对话框
48 System.out.println( " zhuangtai==== " + state);
49 } else {
50 System.out.println( " ~~~~~~~~~~~~~ " + 1 );
51 state = chooser.showSaveDialog( null ); // 显示保存文件对话框
52 }
53
54 File file = chooser.getSelectedFile(); // 得到选择的文件
55 if ( true == ( file != null ) ) {
56 System.out.println(file.toString());
57 }
58 if (file != null && state == JFileChooser.APPROVE_OPTION) { // 选择了文件并点击了打开可保存按钮
59 JOptionPane.showMessageDialog( null , file.getPath()); // 显示提示信息
60 } else if (state == JFileChooser.CANCEL_OPTION) { // 点击了撤销按钮
61 JOptionPane.showMessageDialog( null , " 退出! " ); // 显示提示信息
62 } else if (state == JFileChooser.ERROR_OPTION) {
63 JOptionPane.showMessageDialog( null , " 错误! " ); // 显示提示信息
64 }
65 }
66 });
67
68 this .setSize( 200 , 100 ); // 设置窗口大小
69 this .setVisible( true ); // 设置窗口可见
70 this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭窗口时退出程序
71 }
72
73 public static void main(String args[]) {
74 new JFileChooserDemo();
75 }
76 }
77
再加个过滤器:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
1
import
java.io.File;
2 import javax.swing.filechooser.FileFilter;
3
4 // 文件过滤器
5
6 public class MyFileFilter extends FileFilter
7 {
8
9 String ends; // 文件后缀
10 String description; // 文件描述文字
11
12 public MyFileFilter (String ends, String description)
13 { // 构造函数
14 this .ends = ends; // 设置文件后缀
15 this .description = description; // 设置文件描述文字
16 }
17
18 public boolean accept (File file)
19 { // 重载FileFilter中的accept方法
20 if (file.isDirectory ()) // 如果是目录,则返回true
21 return true ;
22 String fileName = file.getName (); // 得到文件名称
23 if (fileName.toUpperCase ().endsWith (ends.toUpperCase ())) // 把文件后缀与可接受后缀转成大写后比较
24 return true ;
25 else
26 return false ;
27 }
28
29 public String getDescription ()
30 { // 返回文件描述文字
31 return description;
32 }
33 }
2 import javax.swing.filechooser.FileFilter;
3
4 // 文件过滤器
5
6 public class MyFileFilter extends FileFilter
7 {
8
9 String ends; // 文件后缀
10 String description; // 文件描述文字
11
12 public MyFileFilter (String ends, String description)
13 { // 构造函数
14 this .ends = ends; // 设置文件后缀
15 this .description = description; // 设置文件描述文字
16 }
17
18 public boolean accept (File file)
19 { // 重载FileFilter中的accept方法
20 if (file.isDirectory ()) // 如果是目录,则返回true
21 return true ;
22 String fileName = file.getName (); // 得到文件名称
23 if (fileName.toUpperCase ().endsWith (ends.toUpperCase ())) // 把文件后缀与可接受后缀转成大写后比较
24 return true ;
25 else
26 return false ;
27 }
28
29 public String getDescription ()
30 { // 返回文件描述文字
31 return description;
32 }
33 }