从 Java 中的匿名类引用变量

发布于 2024-09-07 07:34:35 字数 328 浏览 3 评论 0原文

我正在编写一个 setonclick 监听器,我希望能够引用该按钮,以便我可以更改其属性。即使其禁用?

我收到此消息:

无法引用在不同方法中定义的内部类中的非最终变量confirmButton

        confirmButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            confirmButton.setEnabled(false);    
        }



    }); 

I am writing a setonclick listner, and I want to be able to refer to that button so that I can change its properties. I.e. make it disabled?

I get thismessage:

Cannot refer to a non-final variable confirmButton inside an inner class defined in a different method

        confirmButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            confirmButton.setEnabled(false);    
        }



    }); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

二智少女 2024-09-14 07:34:35

这是因为您可能试图从以这种方式使用的匿名类访问该按钮:

button.addActionListener(
  new MyListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      //do your things on button }
    }
  }
);

这不起作用,因为在 Java 中,匿名类无法看到在声明它们的方法中声明的变量,因为它们的作用域是分开的。让您的类看到它的唯一方法是强制使用 final 约束,该约束可确保编译器变量在初始化后不会更改,从而允许其将其范围扩展到匿名类。

要快速解决此问题,您可以从 actionPerformed 内的 ActionEvent 访问按钮:

((JButton)e.getSource()).setEnabled(false)

否则,您必须在某处具体声明您的 ActionListener 或声明按钮在具有 staticfinal 属性的方法之外。特别是如果您计划通过另一个元素触发的操作来修改某些元素。

This because you are probably trying to access that button from an anonymous class that you use in this way:

button.addActionListener(
  new MyListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      //do your things on button }
    }
  }
);

This doesn't work because in Java anonymous classes cannot see variables declared in methods in which they are declared too since their scope are separated. The only way to let your class see it is forcing the final constraint which assures the compiler that the variable won't change after being initialized, allowing it to extend its scope to the anonymous classes.

To quickly fix this you can access the button from the ActionEvent inside the actionPerformed:

((JButton)e.getSource()).setEnabled(false)

Otherwise you have to concretely declare your ActionListener somewhere or declare the buttons outside the method with static or final attribute.. especially if you plan to modify some elements by an action that is fired by another element.

羞稚 2024-09-14 07:34:35

我建议不要使用 getSource;该文档并不保证它将成为您的按钮。您可以使按钮在作用域中成为final,或者使用更复杂的类。

public class ComponentRelevantOnClickListener implements View.OnClickListener {

  private JComponent component;

  public ComponentRelevantOnClickListener(JComponent component) {
    this.component = component;
  }
}

// then, in your code...

confirmButton.setOnClickListener(new ComponentRelevantOnClickListener(confirmButton) {

    public void onClick(View view) {
        component.setEnabled(false);    
    }
});

如果您转向操作和侦听器类的设计而不是匿名子类,那么您将获得更多重用的机会(您已经可以看到 ComponentRelevantOnClickListener 可以替换为“DisableOnClickListneer”,您可以在任何地方使用它来实现此目的),并且您的代码总体上将得到更好的设计。

I would recommend against the getSource; the documentation doesn't promise that it will be your button. You can either make your button final in the scope, or use a more sophisticated class

public class ComponentRelevantOnClickListener implements View.OnClickListener {

  private JComponent component;

  public ComponentRelevantOnClickListener(JComponent component) {
    this.component = component;
  }
}

// then, in your code...

confirmButton.setOnClickListener(new ComponentRelevantOnClickListener(confirmButton) {

    public void onClick(View view) {
        component.setEnabled(false);    
    }
});

If you move toward a design of action and listener classes instead of anonymous subclasses, you get more chance for re-use (you can already see that ComponentRelevantOnClickListener could be replaced with a "DisableOnClickListneer" that you can use anywhere for this purpose), and your code will be overall better designed.

无边思念无边月 2024-09-14 07:34:35

在 Java 中,匿名类中引用的变量需要定义为 final。 Jon Skeet 在这篇文章中提供了一个很好的例子。

vars that are referenced within anonymous classes need to be defined as final in Java. Jon Skeet has a great example of this nestled within this article.

无可置疑 2024-09-14 07:34:35

匿名内部类只能访问外部作用域中的变量(如果它们是final)。假设您只分配给 confirmButton 一次,我建议只需将其标记为 final

final JButton confirmButton = new JButton();

Anonymous inner classes can only access variables from the outer scope if they are final. Assuming you only assign to the confirmButton once, I suggest simply tagging it as final.

final JButton confirmButton = new JButton();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文