【问题标题】:Getting error using nested ActionListener class for a JButton Object?使用 JButton 对象的嵌套 ActionListener 类时出错?
【发布时间】:2014-05-25 01:28:57
【问题描述】:

这是我第一次尝试使用动作监听器和事件处理。我正在尝试创建一个具有 3 个按钮的简单 GUI:homeabouttag。对于每个JButton,我添加了一个监听器对象并为监听器对象创建了一个嵌套类。

public class interfacetest {

    public static void main(String[] args) {

        JFrame window = new JFrame("GUI Test");
        window.setSize(250, 100);
        window.setLocation(100, 100);

        final JButton home = new JButton("Home");
        final JButton about = new JButton("About");
        final JButton tag = new JButton("Tag");

        JMenuBar menu = new JMenuBar();
        menu.add(home);
        menu.add(about);
        menu.add(tag);
        menu.setVisible(true);
        window.setJMenuBar(menu);

        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        thehandler handler = new thehandler();
        home.addActionListener(handler);
        about.addActionListener(handler);
        tag.addActionListener(handler);

        window.setVisible(true);
    }

    // Here is my nested class.

    private class thehandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {

            String string = "";
            if (event.getSource()==home)
                string = string.format("home: %s, event.getActionCommand()");
            else if (event.getSource()==about)
                string = string.format("about: %s, event.getActionCommand()");
            else if (event.getSource()==tag) string = string.format("tag: %s, event.getActionCommand()");
            JOptionPane.showMessageDialog(null, string);
        }
    }
}

我在创建一个新的thehandler 对象时收到错误消息: “没有可访问类型为 interfacetest 的封闭实例。必须使用封闭实例限定分配interfacetest 类型的(例如 x.new A() 其中 x 是 interfacetest 的一个实例)。"

【问题讨论】:

  • 什么编译器/lint 会产生如此冗长的消息?从您的帖子中很难推断出您的班级结构。你的 main() 方法属于哪个类?
  • 抱歉没有说清楚。但是我已经在代码中添加了这个类。 Main() 属于:interfacetest 类。我也使用eclipse。
  • 那么你的信息很清楚:你需要一个outer 类的实例来实例化它的inner 类。看看Inner/Outer class obj.new

标签: java event-handling actionlistener nested-class


【解决方案1】:

你有两种简单的方法来解决这个问题:

  1. 将嵌套类设为静态:private static class thehandler
  2. 创建封闭类的实例:(new interfacetest()).new thehandler()(您可能需要重构代码,并且可能希望保留对 interfacetest 实例的引用。

像这样:

interfacetest iner = new interfacetest();
// Do things...
thehandler handler = iner.new thehandler(); // (e.g. x.new A() where x is an instance of interfacetest.)

解释:您的嵌套类是非静态的,但您尝试访问它,就好像它是一样的。

  • 如果类的成员不是静态的,您必须通过该类的实例来访问它:classInstance.nonStaticMember(上面的选项 2)。
  • 如果类的成员是静态的,您可以通过任何一种方式访问​​它,但正确的方式是通过类类型:ClassType.staticMember(上面的选项 1)。

在您的情况下,您正在执行ClassType.nonStaticMemberthis.nonStaticMember 之类的操作(“没有可访问类型interfacetest 的封闭实例”)。

您可能还会发现这个术语(来自tutorials)很有用:

嵌套类分为两类:静态和非静态。声明为静态的嵌套类称为静态嵌套类。非静态嵌套类称为内部类

注意事项:

  • 在您的JFrame 上调用pack() 而不是手动setSize(...)
  • 只有在您对布局进行了所有更改后,才能致电setVisible(true)
  • 当您的按钮执行不同的操作时,为每个按钮创建一个动作侦听器(这样更有意义)。
  • 如果您的按钮执行类似的操作,例如仅使用不同颜色创建相同的面板,请创建一个带有颜色的构造函数的单个动作侦听器(为其创建一个字段),并将其应用到actionPerformed
  • 在 Java 的命名约定中,类名以大写字母开头。

【讨论】:

    猜你喜欢
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 2020-11-01
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多