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 按钮的自定义文件选择器对话框
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());
}
}
文件选择对话框
最新推荐文章于 2024-02-21 17:13:40 发布