事件处理【总结】

时间:2022-04-28 00:22:03

1. 事件类之间的继承关系?

答:

事件处理【总结】

2. 各个事件及其相关方法?

答:

事件处理【总结】

3. 事件监听器的注册?

答:

1)注册事件监听器

    处理事件的基本方法,是事件监听器的注册,即将组建对象和监听器对象相联系。只需要使用对象的addXXXListener方法,当事件源发生某种类型的事件时,则触发事先已注册过的监听器中相应的处理程序。

事件处理【总结】

2)一个对象注册对个监听器

    事件源可以产生多种不同类型的事件,因而可以注册(触发)多种不同类型的监听器。例如,程序中frame对象注册m1和m2两个监听器,即在一个框架中移动鼠标,或者拖动鼠标并画出痕迹,而且框架下方都显示鼠标的位置:

import java.awt.*;import java.awt.event.*;

public class TestMultiListener {
public static void main(String[] args) {
Frame frame=new Frame("Test");
TextField msg=new TextField(50);
Monitor1 m1=new Monitor1(frame);
Monitor2 m2=new Monitor2(frame, msg);
frame.addWindowListener(m1);
frame.addMouseMotionListener(m2);
frame.add(msg, BorderLayout.SOUTH);
frame.setSize(200,160);
frame.setVisible(true);
}
}
class Monitor1 implements WindowListener{
private Frame frame;
public Monitor1(Frame frame) {
this.frame=frame;
}
public void windowClosing(WindowEvent e) {System.exit(0);}
public void windowOpened(WindowEvent e) {}//windowListener有7个方法,即使不做任何事,也要书写
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
}
class Monitor2 implements MouseMotionListener{
private Frame frame;
private TextField msg;
private boolean bDragged=false;
public Monitor2(Frame frame,TextField msg) {
this.frame=frame;
this.msg=msg;
}
public void mouseMoved(MouseEvent e) {
msg.setText("MouseMoved:"+e.getX()+","+e.getY());
if(bDragged) {
msg.setText("MouseMoved:"+e.getX()+","+e.getY());
bDragged=false;
}
}
public void mouseDragged(MouseEvent e){
msg.setText("MouseDragged:"+e.getX()+","+e.getY());
if(!bDragged) {
frame.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));//设置鼠标形态为十字星
bDragged=true;
}
frame.getGraphics().drawLine(e.getX(), e.getY(), e.getX(), e.getY());
}
}

事件处理【总结】                                        事件处理【总结】

3)多个对象注册到一个监听器

import java.awt.*;import java.awt.event.*;public class TestMultiObjectOneListener {	public static void main(String[] args) {		Frame frame=new Frame("Test");		Button b1=new Button("start");		TextField b2=new TextField("input text:");				Monitor3 m=new Monitor3();		b1.addActionListener(m);		b2.addActionListener(m);		frame.add(b1, "North");		frame.add(b2,"Center");		frame.pack();		frame.setVisible(true);	}}class Monitor3 implements ActionListener{	public void actionPerformed(ActionEvent e) {		System.out.println("a button\n"+e.getSource()		+"\nhas been pressed.\n"+				"command is: "+e.getActionCommand()		);	}}

事件处理【总结】


4. 事件适配器(Adapter)?

答:用实现接口的方法,必须实现接口所规定的的方法,如上面说的windowListener,为简化编程,为一些事件监听器接口定义了相应的实现类——事件适配器类,有七种:

ComponentAdapter,ContainerAdapter,FocusAdapter,KeyAdapter,MouseAdapter,MouseMotionAdapter 和 WindowAdapter。

在定义监听器时就可以继承Adapter类,并只重写所需要的方法。例如,将3.2例中Monitor1改为

class Monitor1 extends WindowAdapter{	private Frame frame;	public Monitor1(Frame frame) {		this.frame=frame;	}	public void windowClosing(WindowEvent e) {		System.exit(0);	}}

5. 内部类作为事件监听器?

答:例如,

import java.awt.*;import java.awt.event.*;public class TestInnerListener {	Frame frame=new Frame("内部类测试");	TextField textField=new TextField(30);	public TestInnerListener() {		frame.add(new Label("请按下鼠标左键并拖动"),"North");		frame.add(textField,"South");		frame.setBackground(new Color(200, 0, 0));//red,green,blue component		frame.addMouseMotionListener(new InnerMonitor());		frame.addMouseListener(new InnerMonitor());		frame.setSize(300, 200);		frame.setVisible(true);	}	public static void main(String[] args) {		Object t=new TestInnerListener();	}	private class InnerMonitor implements MouseMotionListener,	MouseListener{		public void mouseDragged(MouseEvent e) {			String string="鼠标拖动到位置("+e.getX()+","+e.getY()+")";			textField.setText(string);		}		public void mouseEntered(MouseEvent e) {			String string="鼠标已进入窗体";			textField.setText(string);		}		public void mouseExited(MouseEvent e) {			String string="鼠标已移出窗体";			textField.setText(string);		}		public void mouseMoved(MouseEvent e) {};//java中一个类只能单继承,所以这里要用实现接口的方法		public void mousePressed(MouseEvent e) {};		public void mouseClicked(MouseEvent e) {};		public void mouseReleased(MouseEvent e) {};	}}

结果:

事件处理【总结】


6. 匿名类作为事件监听器?

答:例如,

import java.awt.*;import java.awt.event.*;public class TestAnonymous {	Frame frame = new Frame("匿名内部类测试");	TextField textField = new TextField(30);		public TestAnonymous() {		frame.add(new Label("请按下鼠标左键并拖动"),"North");		frame.add(textField, "South");				frame.addMouseMotionListener(new MouseMotionListener() {			public void mouseMoved(MouseEvent e) {				String string="鼠标拖动到位置("+e.getX()+","+e.getY()+")";				textField.setText(string);			}			public void mouseDragged(MouseEvent e) {}		});		frame.addWindowListener(new WindowAdapter() {			public void windowClosing(WindowEvent e) {				System.exit(0);			}		});				frame.setSize(300, 200);		frame.setVisible(true);	}		public static void main(String[] args) {		TestAnonymous t = new TestAnonymous();	}}