GUI編程及文件對話框的使用

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;



@SuppressWarnings("serial")
public class Filechooser extends JFrame implements ActionListener{

	JButton open=null;
	public static void main (String[] args){
		new Filechooser();//構造一個指向用戶的默認目錄Filechooser
	}
	public Filechooser(){
		open=new JButton("打開");
		this.add(open);
		this.setBounds(100,100,100,100);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		open.addActionListener(this);
	}
	public void actionPerformed(ActionEvent e){
		JFileChooser j=new JFileChooser();
		j.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//設置Filechooser
		j.showDialog(new JLabel(),"選擇");// 彈出具備本身定義 approve button的本身定義文件選擇器對話框
		File file=j.getSelectedFile();
			if(file.isDirectory()){
				System.out.println("目錄        "+file.getAbsolutePath());
				
			}else if(file.isFile()){
				System.out.println("目錄        "+file.getAbsolutePath());
			}
			System.out.println(j.getSelectedFile().getName());
		
	}
}
<img src="http://img.blog.csdn.net/20150703133924135?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYXlha2FrYXpl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
相關文章
相關標籤/搜索