【问题标题】:Implementing an inner anonymous action listener实现一个内部匿名动作监听器
【发布时间】:2014-04-03 11:59:58
【问题描述】:

我一直在尝试实现上述动作监听器,但不断收到两个错误:

-Cannot instantiate the type ActionListener
-void is an invalid type for the variable incrementAction

我一直在查找类似的示例,但它们似乎都指向了相同的实现方法。 这就是我必须要去的地方。

        increment.addActionListener(new ActionListener());{
        public void incrementAction(ActionEvent e){
            this.incrementCount();
            this.setTextField();
        }
    }

【问题讨论】:

  • 你为什么有;?你认为匿名类定义从哪里开始?
  • 哦,对不起,不应该在那里。

标签: java swing actionlistener inner-classes anonymous-class


【解决方案1】:

ActionListener方法的签名是:

public void actionPerformed(ActionEvent e)

JButton increment  = new JButton();
increment.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("ActionEvent received! ");
    }
});

【讨论】:

  • 为此干杯,我遇到的最后一个问题是我似乎无法使用我在同一个类中创建的 .incrementCount 和 .setTextField 这两种方法。
猜你喜欢
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-05
  • 1970-01-01
  • 2015-10-07
  • 1970-01-01
  • 2021-03-13
相关资源
最近更新 更多