【问题标题】:FlowControl in ActionListener method in Java When JButton Clicked单击JButton时Java中ActionListener方法中的FlowControl
【发布时间】:2013-04-24 12:47:00
【问题描述】:

请有人解释一下 ActionListener 的控制流程,例如我们在注册了一个组件的类中实现了 t 并且确实覆盖了

 actionPerformed(ActionEvent e) 

现在我很困惑,当我们点击addActionListener (this);中的注册按钮what is the role of (this)

【问题讨论】:

标签: java swing actionlistener actionevent


【解决方案1】:

addActionListener (this)表示当前类实现了ActionListener接口,并提供了actionPerformed(ActionEvent e)的实现

示例:

public class SampleListener implements ActionListener{
    public static void main(String[] args) {
        JButton btn1 = new JButton("Click me");
        btn1.addActionListener(this);
    }
    @Override
    public void actionPerformed(ActionEvent event) {
        // code
    }
}

【讨论】:

  • 对处理动作事件感兴趣的类实现了这个接口,“用那个类创建的对象注册到一个组件”这是整个类的对象?请你给我一个例子来解释(这个)是如何成为全班的对象的吗?
  • btn1.addActionListener(this);所以这里(这个)是我全班的对象吗?在我的代码中的任何地方我都可以做到这一点 .mymethod ();是这样的吗?
  • this 表示当前对象不是类。
猜你喜欢
  • 1970-01-01
  • 2016-08-25
  • 1970-01-01
  • 2012-02-07
  • 2022-06-30
  • 1970-01-01
  • 2012-03-07
  • 2018-10-22
  • 1970-01-01
相关资源
最近更新 更多