打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
论坛: Java 中关于选择文件夹对话框 ...
Re: Java 中关于选择文件夹对话框
 
回复
默认情况下没有Windows那种竖排的文件夹选择框,但可以通过FileDialog或JFileChooser设定参数令文件选取方式为[文件夹],给您写了个小例子。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class MyFrame extends JFrame implements ActionListener {

/**
*
*/
private static final long serialVersionUID = 1L;

private JFileChooser fileChooser = new JFileChooser(".");

private JButton button = new JButton("打开文件");

public MyFrame() {
this.setTitle("文件夹选择");
this.setPreferredSize(new Dimension(200, 100));
this.getContentPane().add(button, BorderLayout.NORTH);
button.addActionListener(this);
this.pack();
this.setLocationRelativeTo(null);
this.setIgnoreRepaint(true);
}

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
// 触发JButton(此例仅设置有一个按钮,多按钮请自行更改)
if (source instanceof JButton) {
openFile();
}

}

public void openFile() {
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setDialogTitle("打开文件夹");
int ret = fileChooser.showOpenDialog(null);
if (ret == JFileChooser.APPROVE_OPTION) {
//文件夹路径
System.out.println(fileChooser.getSelectedFile().getAbsolutePath());
}
}

public static void main(String[] args) {
Frame frame = new MyFrame();
frame.setVisible(true);
}


}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
VideoPlayDemo
文件选择对话框,颜色选择对话框,分隔线的使用与介绍 - JAVA记事 - JavaEye技...
Swing使用JFileChooser选择文件目录
设置背景图,并在上面添加可随意拖动的图片标签
Swing文件下载
FileDialog vs JFileChooser
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服