日期:2014-05-20 浏览次数:20685 次
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借鉴哈