GUI编程day05
窗口监听
public class TestWindow {
public static void main(String[] args) {
WindowFrame windowFrame = new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame() {
setBackground(Color.PINK);
setVisible(true);
setBounds(400, 100, 400, 500);
this.addWindowListener(
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
}
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活了");
System.out.println("windowActivated");
}
}
);
}
}
键盘监听
public class TestKeyListener {
public static void main(String[] args) {
KeyFrame keyFrame = new KeyFrame();
}
}
class KeyFrame extends Frame {
public KeyFrame() {
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
System.out.println(keycode);
if(keycode == KeyEvent.VK_UP){
System.out.println("你按下了上建");
}
}
});
}
}