Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JButton对象的嵌套ActionListener类获取错误?_Java_Event Handling_Actionlistener_Nested Class - Fatal编程技术网

Java 使用JButton对象的嵌套ActionListener类获取错误?

Java 使用JButton对象的嵌套ActionListener类获取错误?,java,event-handling,actionlistener,nested-class,Java,Event Handling,Actionlistener,Nested Class,这是我第一次尝试使用动作侦听器和事件处理。我正在尝试创建一个简单的GUI,它有3个按钮:home、about和tag。对于每个JButton,我都添加了一个侦听器对象,并为侦听器对象创建了一个嵌套类 public class interfacetest { public static void main(String[] args) { JFrame window = new JFrame("GUI Test"); window.setSize(250

这是我第一次尝试使用动作侦听器和事件处理。我正在尝试创建一个简单的GUI,它有3个按钮:
home
about
tag
。对于每个
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);
        }
    }
}

我在创建一个新的
处理程序
对象时出错,该对象说:“无法访问interfacetest类型的封闭实例。必须使用interfacetest类型的封闭实例(例如x.new a(),其中x是interfacetest的实例)来限定分配。”

您有两种简单的方法来解决此问题:

  • 将嵌套类设为静态:
    私有静态类处理程序
  • 创建封闭类的实例:
    (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.nonStaticMember
    ,或
    this.nonStaticMember
    (“无法访问interfacetest类型的封闭实例”)

    您还可能会发现(从中)此术语非常有用:

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

    注意事项:

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

    是什么编译器/lint生成了如此详细的消息?从您的帖子中很难推断出您的类结构。main()方法属于哪个类?很抱歉没有说清楚。但我已经在code.main()中添加了该类属于:interfacetest类。我也使用eclipse。那么您的消息就很清楚了:您需要一个
    外部
    类的实例来实例化其
    内部
    类。请查看。可能的