我有以下代码,我在其中向 JFrame 添加了一个 WindowListener ,并且我想覆盖方法 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()
有什么问题? 最佳答案
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");
}
});
我知道我讨厌适配器类是有充分理由的。我建议使用监听器而不是适配器。
关于java - 为什么只有 windowGainedFocus() 不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29398400/