java中檔案儲存、開啟檔案對話方塊

來源:互聯網
上載者:User

標籤:

  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 }

 

java中檔案儲存、開啟檔案對話方塊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.