窗体事件(WindowEvent)、鼠标事件(MouseEvent)、键盘事件(KeyEvent)、动作事件(ActionEvent)
创建事件监听器对象时可以有两种方式:
以窗体事件作为例子:
实现WindowListener接口和继承WindowAdapter事件适配器
以下的例子都是用匿名内部类实现事件处理的
窗体事件(WindowEvent)
package gui;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Example05 {
public static void main(String[] args) {
final Frame f=new Frame("WindowEvent");
f.setSize(400,300);
f.setLocation(300,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() { //这里还可以实现WindowListener接口
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("windowOpended---窗体打开事件");
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println(" windowIconified---窗体图标化事件");
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("windowDeiconified---窗体取消图标化事件");
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("windowDeactivated---窗体停用事件");
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
Window window=e.getWindow();
window.setVisible(false);
window.dispose();
System.out.println("windowClosing---窗体正在关闭事件");
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("windowClosed---窗体关闭事件");
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("windowActivated---窗体激活事件");
}
});
}
}
鼠标事件(MouseEvent)
package gui;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Example06 {
public static void main(String[] args) {
final Frame f=new Frame("WindowEvent");
f.setLayout(new FlowLayout());
f.setSize(300,200);
f.setLocation(300,200);
f.setVisible(true);
Button but=new Button("Button");
f.add(but);
f.addWindowListener(new WindowAdapter(){ //这里也可以使用WindowListener接口
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
Window window=e.getWindow();
window.dispose();
}
});
but.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mouseReleased---鼠标放开事件");
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mousePressed---鼠标按下事件");
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mouseExited---鼠标移除按钮区域事件");
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("mouseEntered---鼠标进入按钮区域事件");
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getButton()==e.BUTTON1){
System.out.println("鼠标左击事件");
}
if(e.getButton()==e.BUTTON2){
System.out.println("鼠标中击事件");
}
if(e.getButton()==e.BUTTON3){
System.out.println("鼠标右击事件");
}
System.out.println("点击次数:"+e.getClickCount());
System.out.println("mouseClicked---鼠标完成点击事件");
}
});
}
}
键盘事件(KeyEvent)
package gui;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Example07 {
public static void main(String[] args) {
final Frame f=new Frame("WindowEvent");
f.setLayout(new FlowLayout());
f.setSize(400,200);
f.setLocation(300,200);
f.setVisible(true);
TextField tf=new TextField(30);
f.add(tf);
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
Window window=e.getWindow();
window.dispose();
}
});
tf.addKeyListener(new KeyAdapter(){
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int keyCode=e.getKeyCode();
String s=KeyEvent.getKeyText(keyCode);
System.out.print("输入的内容为:"+s+",");
System.out.println("对应的keyCode为:"+keyCode);
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("keyReleased---释放某个键事件");
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("keyTyped---键入某个键事件");
}
});
}
}
动作事件(ActionEvent)
动作事件不代表某个具体的动作,只是代表某个动作发生了