实现java.awt.event.ActionListener
接口的最佳方式是什么?
让您的班级实现 ActionListener 并将其添加为 ActionListener:
class Foo implements ActionListener{
public Foo() {
JButton button = new JButton();
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
}
}
或者添加一个匿名 ActionListener 类的对象:
class Foo{
public Foo() {
JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
}