图解应用集成开发环境设计GUI程序

JFrameChoose内置有“打开”和“存储”两种对话框,还可以自己定义其他种类的对话框。下面就是一个文件现在器的常用操作的例子。

效果如图:

选择打开文件按钮

选择保存文件对话框

对话框操作实例程序代码如清单:

 1 package com.javaapp.ch12;
 2 import java.awt.*;
 3 import java.awt.event.*;
 4 import java.io.*;
 5 import javax.swing.*;
 6 import org.jdesktop.application.Application;
 7 
 8 @SuppressWarnings("serial")
 9 public class FramechooseFileDemo extends JFrame {   
10     private BorderLayout borderLayout1 = new BorderLayout();
11     private JPanel jPanel1 = new JPanel();
12     private JLabel jLabel1 = new JLabel();
13     private JTextField jTextField1 = new JTextField();
14     private JLabel jLabel2 = new JLabel();
15     private JTextField jTextField2 = new JTextField();
16     private JButton jButton1 = new JButton();
17     private JButton jButton2 = new JButton();
18     private FlowLayout flowLayout1 = new FlowLayout();
19     public FramechooseFileDemo() {
20         try {
21             Init();
22             Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(getContentPane());
23         } catch (Exception exception) {
24             exception.printStackTrace();
25         }
26     }
27     private void Init() throws Exception {         //初始化组件
28         getContentPane().setLayout(borderLayout1);
29         jLabel2.setToolTipText("");
30         jLabel2.setText("保存文件");
31         jButton1.setText("....");
32         jButton2.setToolTipText("");
33         jButton2.setText("....");
34         jTextField1.setPreferredSize(new Dimension(204,22));
35         jTextField1.setToolTipText("");
36         jTextField2.setPreferredSize(new Dimension(204,22));
37         jLabel1.setToolTipText("");
38         this.setTitle("文件选择器(JFileChooser)");
39         jPanel1.setPreferredSize(new Dimension(339,138));
40         this.getContentPane().add(jPanel1,java.awt.BorderLayout.CENTER);
41         jPanel1.add(jLabel1,null);
42         jPanel1.add(jTextField1,null);
43         jPanel1.add(jButton1,null);
44         jPanel1.add(jLabel2,null);
45         jPanel1.add(jTextField2,null);
46         jPanel1.add(jButton2,null);
47         jLabel1.setText("打开文件");
48         jPanel1.setLayout(flowLayout1);
49         jButton1.addActionListener(new java.awt.event.ActionListener() {
50             public void actionPerformed(ActionEvent e) {
51                 openfile();
52             }
53         });
54         jButton2.addActionListener(new java.awt.event.ActionListener() {
55             public void actionPerformed(ActionEvent e) {
56                 savefile();
57             }
58         });
59     }
60     void openfile() {        //打开文件
61         JFileChooser fc = new JFileChooser();
62         fc.setDialogTitle("选择文件演示");
63         fc.setDialogType(JFileChooser.CUSTOM_DIALOG);
64         if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
65             this.jTextField1.setText(fc.getSelectedFile().toString());
66             this.jTextField2.setText(fc.getSelectedFile().toString());
67         }
68     }
69     void savefile() {     //保存文件
70         JFileChooser fc = new JFileChooser();
71         fc.setDialogTitle("保存文件演示");
72         fc.setDialogType(JFileChooser.SAVE_DIALOG);
73         File f1 = new File(jTextField1.getText());
74         fc.setCurrentDirectory(f1);
75         fc.setControlButtonsAreShown(true);
76         fc.setSelectedFile(f1);
77         if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
78         }
79     }
80     public static void main(String[] args) {
81         FramechooseFileDemo frame = new FramechooseFileDemo();
82         frame.pack();
83         frame.setVisible(true);  //启动窗口应用程序,使窗口可见
84     }
85 }

程序分析:
    在程序中,创建了打开文件和保存文件对话框。创建一个文件对话框的方法为:

1 JFileChooser fc = new JFileChooser();
2 fc.setDialogTitle("选择文件演示");

然后设置文件对话框的类型,设置方法为:

1 fc.setDialogType(JFileChooser.CUSTOM_DIALOG);//设置为打开文件对话框
2 fc.setDialogType(JFileChooser.SAVE_DIALOG);//设置为保存文件对话框

显示文件对话框并接受用户所选择对话框中按钮的方法为:

1 if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {//表示用户正确的选择了文件操作

     如果选择的是打开文件对话框,可以用JFileChoose的getSeletedFile方法获得所选的文件。对于保存文件对话框,可以用JFileChoose的setCurrentDirectory方法设置当前所要保存文件的路径。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值