I want have a very easy effect: create a window, then if I press a button, it will be closed.
So I write the following code, but it doesn't work.
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class test
{
public static void main(String[] args)
{
final Window wnd = new Window(new Frame());
wnd.setLocation(100, 100);
wnd.setSize(400,300);
wnd.setVisible(true);
wnd.requestFocusInWindow();
wnd.addKeyListener(
new KeyAdapter() {
public void keyPressed(KeyEvent event)
{
wnd.setVisible(false);
wnd.dispose();
System.exit(0);
}
}
);
}
}
I guess the problem may be lie in:
wnd.requestFocusInWindow();
because the returned value of wnd.requestFocusInWindow() is always "false". Why? How can I fix the problem? (I use ubuntu and Eclipse.)