當定義一個簡單的點擊JButton的行爲時,這是正確的方法嗎?而且,有什麼區別?addMouseListener或addActionListener或JButton?
JButton but = new JButton();
but.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You clicked the button, using an ActionListener");
}
});
或
JButton but = new JButton();
but.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
System.out.println("You clicked the button, using a MouseListenr");
}
});