JAVA 中打开/保存开文件对话框 源代码

JAVA 中打开/保存开文件对话框 源代码

  (2009-07-08 08:17:52)
标签: 

杂谈

分类: java学习
  1. //AWT: FileDialog类 + FilenameFilter类 可以实现本功能
    //Swing: JFileChooser类 + FileFilter类     可以实现本功能
  2. //
  3. //该类用来测试打开文件和保存文件的对话框   
  4.   
  5. import java.awt.*;      //为了使用布局管理器   
  6. import java.awt.event.*;//用来处理事件   
  7. import javax.swing.*;   //最新的GUI组件   
  8. import java.io.*;       //读写文件用   
  9.   
  10. public class filechooser   
  11.   
  12.   
  13.     private JFrame frm;   
  14.     private JButton open;   
  15.     private JButton read;   
  16.     private JPanel p;   
  17.     private File f;   
  18.     private JFileChooser fc;   
  19.     private int flag;   
  20.   
  21.    public filechooser()   
  22.     {   
  23.       frm=new JFrame("java");   
  24.       open=new JButton("open");   
  25.       read=new JButton("read");   
  26.       p=new JPanel();   
  27.       fc=new JFileChooser();   
  28.   
  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.   
  48.   private void openFile()  //打开文件   
  49.            {   
  50.               //设置打开文件对话框的标题   
  51.                 fc.setDialogTitle("Open File");   
  52.   
  53.               //这里显示打开文件的对话框   
  54.            try   
  55.                        flag=fc.showOpenDialog(frm);    
  56.                 }   
  57.            catch(HeadlessException head){    
  58.   
  59.                        System.out.println("Open File Dialog ERROR!");   
  60.                 }   
  61.                  
  62.               //如果按下确定按钮,则获得该文件。   
  63.               if(flag==JFileChooser.APPROVE_OPTION)   
  64.                 {   
  65.                      //获得该文件   
  66.                        f=fc.getSelectedFile();   
  67.                        System.out.println("open file----"+f.getName());   
  68.                  }   
  69.            }   
  70.   
  71.   private void readFile() //保存文件   
  72.          {   
  73.              String fileName;   
  74.   
  75.            //设置保存文件对话框的标题   
  76.              fc.setDialogTitle("Save File");    
  77.   
  78.            //这里将显示保存文件的对话框   
  79.         try   
  80.                   flag=fc.showSaveDialog(frm);   
  81.              }   
  82.         catch(HeadlessException he){   
  83.                   System.out.println("Save File Dialog ERROR!");    
  84.              }   
  85.   
  86.            //如果按下确定按钮,则获得该文件。   
  87.            if(flag==JFileChooser.APPROVE_OPTION)   
  88.              {   
  89.                  //获得你输入要保存的文件   
  90.                    f=fc.getSelectedFile();   
  91.                  //获得文件名   
  92.                    fileName=fc.getName(f);   
  93.                  //也可以使用fileName=f.getName();   
  94.                    System.out.println(fileName);   
  95.              }   
  96.          }   
  97.   
  98.   //按钮监听器类内部类   
  99.   class action implements ActionListener   
  100.     {   
  101.       public void actionPerformed(ActionEvent e)   
  102.         {   
  103.   
  104.           //判断是哪个按纽被点击了   
  105.           if(e.getSource()==open)   
  106.                openFile();   
  107.           else  
  108.           if(e.getSource()==read)   
  109.                readFile();   
  110.         }   
  111.     }   
  112.   
  113.   public static void main(String[] args)   
  114.     {   
  115.     new filechooser();   
  116.      }   
  117. }
---------------------------------------------
JFileChooser fc new JFileChooser() ;
fc.showSaveDialog(this) ;
File fc.getSelectFile() ;
//然后对f做输出流
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值