我有一个Java Applet(使用JApplet类),我试图在Applet窗口被激活时获取一个事件。我试图实现WindowListener和FocusListener,但我没有收到任何事件。我需要做些什么才能确定applet窗口是否已获得焦点?
这是我的测试代码:
public class AppletTest extends JApplet implements WindowListener, FocusListener {
public void init() {
// addWindowListener(this); // Won't compile
addFocusListener(this);
}
public void actionPerformed(ActionEvent event){
Object source = event.getSource();
String command = event.getActionCommand();
}
@Override
public void focusGained(FocusEvent e) {
System.out.println("focusGained");
}
@Override
public void focusLost(FocusEvent e) {}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
}
// Stub methods
@Override
public void windowOpened(WindowEvent e) {}
@Override
public void windowClosing(WindowEvent e) {}
@Override
public void windowClosed(WindowEvent e) {}
@Override
public void windowIconified(WindowEvent e) {}
@Override
public void windowDeiconified(WindowEvent e) {}
@Override
public void windowDeactivated(WindowEvent e) {}
}
答案 0 :(得分:1)
添加: setFocusable(真);
到init方法应该解决你的问题。