JButton和JFileChooser的错误消息 [英] error message with JButton and JFileChooser

查看:66
本文介绍了JButton和JFileChooser的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

此处的ActionListener明确地抛出UnsupportedOperationException.将JFileChooser功能移到ActionListener:

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

这篇关于JButton和JFileChooser的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆