【问题标题】:error message with JButton and JFileChooserJButton 和 JFileChooser 的错误消息
【发布时间】:2013-05-14 22:18:53
【问题描述】:

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

【问题讨论】:

    标签: java swing jbutton runtime-error jfilechooser


    【解决方案1】:

    这里的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();
                ...
            }
        }
    });
    

    【讨论】:

    • 感谢您的帮助。通过这些更改,我得到了这个错误:“线程中的异常“AWT-EventQueue-0”java.lang.NullPointerException”
    • 已更新。为了读者的利益,请发布您的实际代码,最好采用可编译的短 SSCCE 格式
    • 在这种情况下,什么是“myFrame”?
    • 我使用了 null 并得到了同样的错误。 “线程中的异常“AWT-EventQueue-0”java.lang.NullPointerException”:(
    • 该错误发生在其他地方。我会说发布另一个问题,但使用更新和可编译的代码
    【解决方案2】:

    ActionListener 接口定义了一个名为actionPerformed 的方法。您的类中有两种方法,一种称为actionPerformed,另一种称为ActionPerformed。被调用的是接口中定义的那个,即actionPerformed。你的类中有这样一个方法,它唯一的语句是抛出一个UnsupportedOperationException。包含真实代码的ActionPerformed 方法永远不会被调用。

    解决方案:

    剥离存根actionPerformed 方法并将ActionPerformed 的名称更改为actionPerformed。或者(虽然不推荐),让actionPerformed 调用ActionPerformed

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-30
      • 1970-01-01
      • 2019-04-19
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多