JAVA选择文件夹路径
JAVA选择文件夹路径、要文件夹的路径,而input type="file"只能选择文件,却选不了文件夹
大侠们。怎么弄?告诉我一下
[解决办法]
用JFileChooser,并且setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
public class DemoJFileChooser extends JPanel implements ActionListener { JButton go; JFileChooser chooser; String choosertitle; public DemoJFileChooser() { go = new JButton("Do it"); go.addActionListener(this); add(go); } public void actionPerformed(ActionEvent e) { int result; chooser = new JFileChooser(); chooser.setCurrentDirectory(new java.io.File(".")); chooser.setDialogTitle(choosertitle); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // // disable the "All files" option. // chooser.setAcceptAllFileFilterUsed(false); // if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory()); System.out.println("getSelectedFile() : " + chooser.getSelectedFile()); } else { System.out.println("No Selection "); } } public Dimension getPreferredSize(){ return new Dimension(200, 200); } public static void main(String s[]) { JFrame frame = new JFrame(""); DemoJFileChooser panel = new DemoJFileChooser(); frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); frame.getContentPane().add(panel,"Center"); frame.setSize(panel.getPreferredSize()); frame.setVisible(true); }}
[解决办法]
这个答案很N借鉴哈