1. Java中awt包
class="java">
AWT事件模型Event
1) 事件-描述发生了什么的对象
2) 事件源-事件的产生器
3) 事件处理器-接收事件、解释事件并处理用户交互的方法
Button是ActionEvent的对象。
事件处理器是一个方法。
之前使用层次模型(现在已经不使用了)。
事件监听器:委托模型。
增加事件处理器button.addActionListener()
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestButton {
public static void main(String[] args){
Frame frame = new Frame("Test Button");
Button button = new Button("Press Me!");
//增加事件处理器
button.addActionListener(new ButtonHandler());
frame.add(button, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
class ButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String label = e.getActionCommand();
System.out.println(label);
}
}
当单击一个按钮时就会产生一个事件(ActionEvent),然后检查是否有与该按钮关联的事件处理器(实际上就是一个方法),如果没有,那么什么都不执行;如果有,就会将该事件传递给与该按钮关联的事件处理器方法,作为该方法的参数,之后该事件处理器方法就会自动得到调用,并且该方法可以使用传递过来的ActionEvent对象,进而获得事件发生时与该事件及事件源相关联的那些信息。
Java中的组件若想添加事件处理器,都会使用形如addXxxListener的方法来添加。
按钮获取时间ActionListener。
关闭按钮的实现WindowListener的windowClosing()用System.exit(0)关闭。
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Date;
public class MyFrame2 {
public static void main(String[] args) {
Frame frame = new Frame("My Frame2");
Button button = new Button("click me");
button.addActionListener(new MyListener());
frame.addWindowListener(new MyWindowListener());
frame.add(button);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
class MyListener implements ActionListener{
@Override
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
long milliSeconds = e.getWhen();
Date date = new Date(milliSeconds);
System.out.println(date.toLocaleString());
}
}
class MyWindowListener implements WindowListener{
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("关闭窗口");
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
MouseMotionListener接口
MouseListener接口
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class TwoListen implements MouseMotionListener, MouseListener{
private Frame frame;
private TextField textField;
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
String str = "The mouse has left the Frame";
this.textField.setText(str);
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
String str = "x: " + e.getX() + ", y: " + e.getY();
this.textField.setText(str);
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
public void go(){
frame = new Frame("Two Listeners Example");
frame.add(new Label("click"), BorderLayout.NORTH);
textField = new TextField();
frame.add(textField, BorderLayout.SOUTH);
frame.addMouseMotionListener(this);
frame.addMouseListener(this);
frame.addMouseListener(new MyMouseListener());
frame.setSize(300, 200);
frame.setVisible(true);
}
public static void main(String[] args) {
TwoListen two = new TwoListen();
two.go();
}
}
class MyMouseListener implements MouseListener{
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
String str = "The mouse has entered the Frame";
System.out.println(str);
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
String str = "The mouse has exited the Frame";
System.out.println(str);
}
}
mouseDragged()鼠标拖动事件
一个事件源对应多个处理器。