我刚刚在一个 uni 项目上收到了一些不好的反馈,需要一些公正的澄清;
谁能解释我什么时候应该使用(匿名)内部监听器类与监听它们自己的组件? (a 与 b)
一)
public class myButton extends JButton {
public myButton() {
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// handling code...
}
});
}
}
二)
public class myButton extends JButton implements ActionListener {
public myButton() {
addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// handle the event
}
}
谢谢大家, 米奇
最佳答案
c)
JButton myButton = new JButton();
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// handling code...
}
});
关于java - Swing 组件监听自身与内部类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5856083/