java jfilechooser报错,JButton和JFileChooser的错误消息
I want to have a button with a JFileChooser action. This is the code that I wrote:public class Main {private static String fullPath;private JFileChooser inputFile;public static void main(String args[]
I want to have a button with a JFileChooser action. This is the code that I wrote:
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 {
}
}
}
but the problem is that when I run it, I got a long error message that is part of:
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)
解决方案
The ActionListener here is explicitly throwing an UnsupportedOperationException. Move the JFileChooser functionality into the ActionListener:
input_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser inputFile = new JFileChooser();
inputfile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if (inputfile.showOpenDialog(myFrame) == JFileChooser.APPROVE_OPTION) {
File file1 = inputFile.getSelectedFile();
String fullpathTemp = (String) file1.getAbsolutePath();
...
}
}
});
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)