gpt4 book ai didi

java - JButton 和 JFileChooser 的错误消息

转载 作者:行者123 更新时间:2023-12-01 23:48:59 27 4
gpt4 key购买 nike

我想要一个带有 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)

最佳答案

此处的 ActionListener 显式抛出 UnsupportedOperationException。将 JFileChooser 功能移至 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();
...
}
}
});

关于java - JButton 和 JFileChooser 的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16553778/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com