我有以下代码,我将WindowListener添加到我的JFrame中,我想覆盖方法windowGainedFocus
:
final JFrame jd = new JFrame();
jd.setLocationRelativeTo(null);
jd.setSize(300, 425);
jd.setLayout(null);
jd.setResizable(false);
jd.addWindowListener(new WindowAdapter() {
public void windowGainedFocus(WindowEvent windowEvent){
System.out.println("TEST");
}
});
但它不起作用,当我对焦这个框架时,它不会打印“TEST”。
但当我覆盖方法windowClosing
时,它可以工作:
jd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.out.println("TEST");
}
});
windowGainedFocus()
有什么问题?
答案 0 :(得分:4)
jd.addWindowListener(new WindowAdapter() {
public void windowGainedFocus(WindowEvent windowEvent){
System.out.println("TEST");
}
});
应该是:
jd.addWindowFocusListener(new WindowAdapter() {
public void windowGainedFocus(WindowEvent windowEvent){
System.out.println("TEST");
}
});
我知道有一个很好的理由我讨厌适配器类..我建议使用监听器而不是适配器。