假設我有一個名爲button1的按鈕。如果我要創建我應該選擇哪一種方法按鈕一個ActionListener:(在第二個,你必須擴展ActionListener接口)添加ActionListener的方法
// Imports
public class Test{
JButton test = new JButton();
Test(){
// Pretend there is an adapter
test.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
...
}
});
...
}
或
// Imports
public class Test2 extends ActionListener{
JButton button2 = new JButton();
Test2(){
button2.addActionListener(this);
}
// Pretend there is an adapter
public void actionPerformed(ActionEvent e){
Object src = e.getSource();
if(src == button2){
...
}else{
...
}
}
+1不會讓您的UI類實現偵聽器接口 – Robin