JFileChooser アクションを含むボタンが必要です。これは私が書いたコードです:
public class Main {
private static String fullPath;
private JFileChooser inputFile;
public static void main(String args[]) throws FileNotFoundException, IOException {
try {
GridBagConstraints gbc = new GridBagConstraints();
JButton inputButton = new JButton("Browse input file");
myPanel.add(inputButton, gbc);
inputButton.addActionListener(new ActionListener() {
public void ActionPerformed(ActionEvent e) {
JFileChooser inputFile = new JFileChooser();
inputFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
File file1 = inputFile.getSelectedFile();
String fullpathTemp = (String) file1.getAbsolutePath();
fullPath = fullpathTemp;
}
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
} finally {
}
}
}
しかし、問題は、実行すると、次の一部である長いエラー メッセージが表示されることです。
Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.
at main.Main$1.actionPerformed(Main.java:200)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)