《Java程式設計》第16周周四:GUI編程及檔案對話方塊的使用

來源:互聯網
上載者:User
package qiang; import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;public class FileChooser extends JFrame implements ActionListener{/** *  */private static final long serialVersionUID = 1L;/** *  */    JButton open=null;JTextField jtfPath=null;public static void main(String[] args){new FileChooser();}public FileChooser(){this.setLayout(new FlowLayout());open=new JButton("open");open.addActionListener(this);this.add(open);JTextField jtfPath = new JTextField("選擇的檔案",40);jtfPath.setEditable(false);jtfPath.setHorizontalAlignment(JTextField.CENTER);this.add(jtfPath);this.setBounds(400,200,100,100);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//open.addActionListener(this);}@Overridepublic void actionPerformed(ActionEvent e) {   JFileChooser jfc=new JFileChooser();   jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );   jfc.showDialog(new JLabel(),"選擇");   File file=jfc.getSelectedFile();   if(file.isDirectory()){   System.out.println("檔案夾:"+file.getAbsolutePath());   }else if(file.isFile()){System.out.println("檔案:"+file.getAbsolutePath());}   System.out.println(jfc.getSelectedFile().getName());   // 把檔案路徑顯示在文字框中       jtfPath.setText(file.getAbsolutePath());}}
<img src="https://img-blog.csdn.net/20150625105310363?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzkyMjkwNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
<img src="https://img-blog.csdn.net/20150625105321176?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzkyMjkwNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

聯繫我們

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