Java按钮事件处理程序无法解析

时间:2016-04-28 12:05:02

标签: java event-handling awt

我正在尝试在GUI上设置偶数处理程序。但是,当我声明该类然后在按钮上调用它时,它无法解析?

btnHigher = new JButton("Higher");
    btnHigher.setBounds(190, 110, 75, 50);
    btnHigher.addActionListener(new HigherHandler());
    panel.add(btnHigher);

处理程序类:     class HigherHandler实现ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

        }

    }

它只是抛出一个错误: HigherHandler无法解析为类型

1 个答案:

答案 0 :(得分:1)

btnHigher = new JButton("Higher");
    btnHigher.setBounds(190, 110, 75, 50);
    btnHigher.addActionListener(new HigherHandler());
    panel.add(btnHigher);

以下行可以写成这样,而不是创建一个新类。

 btnHigher.addActionListener(new HigherHandler(){
       // it will ask you to add implemented method here which is action performed method.

});