java中文件保存、打开文件对话框

  1 package com.soft.test;
  2 
  3 //AWT: FileDialog类 + FilenameFilter类 可以实现本功能
  4 //Swing: JFileChooser类 + FileFilter类 可以实现本功能
  5 //
  6 //该类用来测试打开文件和保存文件的对话框   
  7 
  8 import java.awt.*; //为了使用布局管理器   
  9 import java.awt.event.*;//用来处理事件   
 10 import javax.swing.*; //最新的GUI组件   
 11 import java.io.*; //读写文件用   
 12 
 13 public class filechooser {
 14 
 15     private JFrame frm;
 16     private JButton open;
 17     private JButton read;
 18     private JPanel p;
 19     private File f;
 20     private JFileChooser fc;
 21     private int flag;
 22 
 23     public filechooser() {
 24         frm = new JFrame("java");
 25         open = new JButton("open");
 26         read = new JButton("read");
 27         p = new JPanel();
 28         fc = new JFileChooser();
 29 
 30         Container c = frm.getContentPane();
 31         c.setLayout(new FlowLayout());
 32 
 33         c.add(p);
 34         p.add(open);
 35         p.add(read);
 36 
 37         //注册按钮事件   
 38         open.addActionListener(new action());
 39         read.addActionListener(new action());
 40 
 41         frm.setSize(300, 300);
 42         frm.setVisible(true);
 43         //设置默认的关闭操作   
 44         frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 45     }
 46 
 47     private void openFile() //打开文件   
 48     {
 49         //设置打开文件对话框的标题   
 50         fc.setDialogTitle("Open File");
 51 
 52         //这里显示打开文件的对话框   
 53         try {
 54             flag = fc.showOpenDialog(frm);
 55         } catch (HeadlessException head) {
 56 
 57             System.out.println("Open File Dialog ERROR!");
 58         }
 59 
 60         //如果按下确定按钮,则获得该文件。   
 61         if (flag == JFileChooser.APPROVE_OPTION) {
 62             //获得该文件   
 63             f = fc.getSelectedFile();
 64             System.out.println("open file----" + f.getName());
 65         }
 66     }
 67 
 68     private void readFile() //保存文件   
 69     {
 70         String fileName;
 71 
 72         //设置保存文件对话框的标题   
 73         fc.setDialogTitle("Save File");
 74 
 75         //这里将显示保存文件的对话框   
 76         try {
 77             flag = fc.showSaveDialog(frm);
 78         } catch (HeadlessException he) {
 79             System.out.println("Save File Dialog ERROR!");
 80         }
 81 
 82         //如果按下确定按钮,则获得该文件。   
 83         if (flag == JFileChooser.APPROVE_OPTION) {
 84             //获得你输入要保存的文件   
 85             f = fc.getSelectedFile();
 86             //获得文件名   
 87             fileName = fc.getName(f);
 88             //也可以使用fileName=f.getName();   
 89             System.out.println(fileName);
 90         }
 91     }
 92 
 93     //按钮监听器类内部类   
 94     class action implements ActionListener {
 95         public void actionPerformed(ActionEvent e) {
 96             //判断是哪个按纽被点击了   
 97             if (e.getSource() == open)
 98                 openFile();
 99             else if (e.getSource() == read)
100                 readFile();
101         }
102     }
103 
104     public static void main(String[] args) {
105         new filechooser();
106     }
107 }
原文地址:https://www.cnblogs.com/xs-yqz/p/4553699.html