默认情况下没有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);
}
}