實現java.awt.event.ActionListener
接口的最佳方式是什麼?您的類應該實現ActionListener還是使用匿名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) {
}
});
}
}
+1 - AbstractAction – 2012-07-11 17:30:22
+1對於'AbstractAction'和['Single Responsibility Principle'](http://en.wikipedia.org/wiki/Single_responsibility_principle)。 – 2012-07-11 17:56:04
不錯,但它似乎更復雜(以代碼維護術語),並且更難以閱讀。或者我錯了? – elias 2012-07-11 17:57:15