我正在尝试在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无法解析为类型
答案 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.
});