Swing實現檔案選擇(目錄選擇)附匯出

來源:互聯網
上載者:User

標籤:style   c   class   blog   java   tar   

具體產生工具

 

 

(1)

(2)

 

(3)

 

 

(4)

 

 

 

 

源碼 :

 

example.java

 

 

[java] view plaincopyprint? 
  1. package org.qiailin.jframe;  
  2. import java.awt.Container;  
  3. import java.awt.Point;  
  4. import java.awt.Toolkit;  
  5. import java.awt.event.ActionEvent;  
  6. import java.awt.event.ActionListener;  
  7. import java.io.File;  
  8. import javax.swing.JButton;  
  9. import javax.swing.JFileChooser;  
  10. import javax.swing.JFrame;  
  11. import javax.swing.JLabel;  
  12. import javax.swing.JOptionPane;  
  13. import javax.swing.JTabbedPane;  
  14. import javax.swing.JTextField;  
  15. /** 
  16.  * 因為看很多朋友在問試用臨時抽時間寫了個簡單的案例 源碼放上去,需要的可以看看,由於時間匆忙做得很粗糙 2011年1月4日23:22:31 
  17.  *  
  18.  * @author 漆艾林 QQ 172794299 郵箱 [email protected] 
  19.  *  
  20.  */  
  21. public class Jexample implements ActionListener {  
  22.     JFrame frame = new JFrame("漆艾林-Example");// 架構布局  
  23.     JTabbedPane tabPane = new JTabbedPane();// 選項卡布局  
  24.     Container con = new Container();//  
  25.     JLabel label1 = new JLabel("檔案目錄");  
  26.     JLabel label2 = new JLabel("選擇檔案");  
  27.     JTextField text1 = new JTextField();// TextField 目錄的路徑  
  28.     JTextField text2 = new JTextField();// 檔案的路徑  
  29.     JButton button1 = new JButton("...");// 選擇  
  30.     JButton button2 = new JButton("...");// 選擇  
  31.     JFileChooser jfc = new JFileChooser();// 檔案選取器  
  32.     JButton button3 = new JButton("確定");//  
  33.       
  34.     Jexample() {  
  35.         jfc.setCurrentDirectory(new File("d://"));// 檔案選取器的初始目錄定為d盤  
  36.           
  37.         double lx = Toolkit.getDefaultToolkit().getScreenSize().getWidth();  
  38.           
  39.         double ly = Toolkit.getDefaultToolkit().getScreenSize().getHeight();  
  40.           
  41.         frame.setLocation(new Point((int) (lx / 2) - 150, (int) (ly / 2) - 150));// 設定視窗出現位置  
  42.         frame.setSize(280, 200);// 設定視窗大小  
  43.         frame.setContentPane(tabPane);// 設定布局  
  44.         label1.setBounds(10, 10, 70, 20);  
  45.         text1.setBounds(75, 10, 120, 20);  
  46.         button1.setBounds(210, 10, 50, 20);  
  47.         label2.setBounds(10, 35, 70, 20);  
  48.         text2.setBounds(75, 35, 120, 20);  
  49.         button2.setBounds(210, 35, 50, 20);  
  50.         button3.setBounds(30, 60, 60, 20);  
  51.         button1.addActionListener(this); // 添加事件處理  
  52.         button2.addActionListener(this); // 添加事件處理  
  53.         button3.addActionListener(this); // 添加事件處理  
  54.         con.add(label1);  
  55.         con.add(text1);  
  56.         con.add(button1);  
  57.         con.add(label2);  
  58.         con.add(text2);  
  59.         con.add(button2);  
  60.         con.add(button3);  
  61.         frame.setVisible(true);// 視窗可見  
  62.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 使能關閉視窗,結束程式  
  63.         tabPane.add("1面板", con);// 添加布局1  
  64.     }  
  65.     /** 
  66.      * 時間監聽的方法 
  67.      */  
  68.     public void actionPerformed(ActionEvent e) {  
  69.         // TODO Auto-generated method stub  
  70.         if (e.getSource().equals(button1)) {// 判斷觸發方法的按鈕是哪個  
  71.             jfc.setFileSelectionMode(1);// 設定只能選擇到檔案夾  
  72.             int state = jfc.showOpenDialog(null);// 此句是開啟檔案選取器介面的觸發語句  
  73.             if (state == 1) {  
  74.                 return;  
  75.             } else {  
  76.                 File f = jfc.getSelectedFile();// f為選擇到的目錄  
  77.                 text1.setText(f.getAbsolutePath());  
  78.             }  
  79.         }  
  80.         // 綁定到選擇檔案,先擇檔案事件  
  81.         if (e.getSource().equals(button2)) {  
  82.             jfc.setFileSelectionMode(0);// 設定只能選擇到檔案  
  83.             int state = jfc.showOpenDialog(null);// 此句是開啟檔案選取器介面的觸發語句  
  84.             if (state == 1) {  
  85.                 return;// 撤銷則返回  
  86.             } else {  
  87.                 File f = jfc.getSelectedFile();// f為選擇到的檔案  
  88.                 text2.setText(f.getAbsolutePath());  
  89.             }  
  90.         }  
  91.         if (e.getSource().equals(button3)) {  
  92.             // 彈出對話方塊可以改變裡面的參數具體得靠大家自己去看,時間很短  
  93.             JOptionPane.showMessageDialog(null, "彈出對話方塊的執行個體,歡迎您-漆艾琳!", "提示", 2);  
  94.         }  
  95.     }  
  96.     public static void main(String[] args) {  
  97.         new Jexample();  
  98.     }  
  99. }  

 

 

 

 

 

 

 

 

這一步注意選擇main方法的類

 

 

簡單的匯出就OK了,打包可以用ant 以及maven ,當然這裡就不介紹了 有興趣的可以去看看

 

 

 

有興趣的可以去下載

 

 

當然如果還有朋友想讓你的jar檔案脫離Java的壞境,可以產生exe的檔案在這裡可以使用exe4j

 

當然這裡exe4j相信大家都會用吧 不會用的話 可以去Google。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.