窗口监听:WindowAdapter
被激活时使用:显示到桌面上
import com.li.changGe.awt.less01.MyFrame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Demo01 extends WindowAdapter{
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
myFrame.addWindowListener(new Demo01());
}
@Override
public void windowActivated(WindowEvent e) {
super.windowActivated(e);
System.out.println("Hello World");
}
}
点击关闭隐藏窗口
能用内部类尽量用内部类
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Demo02 extends Frame{
public static void main(String[] args) {
new Demo02();
}
public Demo02(){
setBounds(0,0,500,500);
setVisible(true);
setResizable(false);
this.addWindowListener(new MyWindow());
}
class MyWindow extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);
}
}
}
键盘监听:KeyAdapter
一个获取对应的数字;一个获取值,有的会乱码
import com.li.changGe.awt.less01.MyFrame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Demo01 {
public static void main(String[] args) {
new Demo01();
}
public Demo01(){
MyFrame myFrame = new MyFrame();
myFrame.addKeyListener(new KeyListeners());
}
class KeyListeners extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println(keyCode);
char result = e.getKeyChar();
System.out.println(result);
}
}
}