Java文件选择对话框

 
JFileChooser jf = new JFileChooser(“Title...”);
jf.setDialogTitle("Chose...");
int result = jf.showOpenDialog(MainFrame.this);
jf.setVisible(true);
File selectedFile = null;
if (result == JFileChooser.APPROVE_OPTION) {
    System.out.println("OK button is pushed.");
    selectedFile = jf.getSelectedFile();
    if (selectedFile.exists()) {
        System.out.println("Yes! You have selected the right file.");
    } else {
        System.out.println("No! You did not select the right file.");
    }
} else if (result == JFileChooser.CANCEL_OPTION) {
    System.out.println("Cancel button is pushed.");
} else if (result == JFileChooser.ERROR_OPTION) {
    System.err.println("Error when select file.");
}

你可能感兴趣的:(java)