AWT事件模型

监听接口实现的三种方法:

AWT实现监听接口的方法有4种:

1)在实现者当中给出接口的每个方法实现。

2)采用接口形式产生匿名对象进行实现

3)采用事件适配器方式。事件适配器就是针对特定的监听接口给出默认实现。

4)采用适配器形式产生匿名对象给出实现。


在实现者当中给出接口的每个方法实现

import java.awt.*;
import java.awt.event.*;
class MyWindowListener implements WindowListener{
	public void windowClosing(WindowEvent e){
		System.out.print("我退出了");
		e.getWindow().setVisible(false);
		((Window)e.getComponent()).dispose();
		System.exit(0);
	}
	public void windowActivated(WindowEvent e){
		
	}
	public void windowClosed(WindowEvent e){
		
	}
	public void windowDeactivated(WindowEvent e){}
	public void windowDeiconified(WindowEvent e){}
	public void windowIconified(WindowEvent e){}
	public void windowOpened(WindowEvent e){}
};
class TestFrame{
	public static void main(String arg[]){
		Frame f=new Frame("A Test Window");
		f.setSize(250,150);
		f.setVisible(true);
		f.addWindowListener(new MyWindowListener());
	}
}
采用接口形式产生匿名对象进行实现

import java.awt.*;
import java.awt.event.*;
public class TestFrame1 {
	public static void main(String arg[]){
		Frame f=new Frame("A Test Window");
		f.setSize(250,150);
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					System.out.println("我退出了!");
					e.getWindow().setVisible(false);
					((Window)e.getComponent()).dispose();
					System.exit(0);
				}
				public void windowActivated(WindowEvent e){}
				public void windowClosed(WindowEvent e){}
				public void windowDeactivated(WindowEvent e){}
				public void windowDeiconified(WindowEvent e){}
				public void windowIconified(WindowEvent e){}
				public void windowOpened(WindowEvent e){}
			}
		);
	}
}
采用事件适配器方式。事件适配器就是针对特定的监听接口给出默认实现。
import java.awt.*;
import java.awt.event.*;
public class TestFrame1 extends WindowAdapter{
	public static void main(String arg[]){
		Frame f=new Frame("A Test Window");
		f.setSize(250,150);
		f.setVisible(true);
		f.addWindowListener(new TestFrame1());
	}
	public void windowClosing(WindowEvent e){
		System.out.print("我退出了");
		e.getWindow().setVisible(false);
		((Window)e.getComponent()).dispose();
		System.exit(0);
	}
}


采用适配器形式产生匿名对象给出实现。

import java.awt.*;
import java.awt.event.*;
public class TestFrame1 {
	public static void main(String arg[]){
		Frame f=new Frame("A Test Window");
		f.setSize(250,150);
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					System.out.println("我退出了!");
					e.getWindow().setVisible(false);
					((Window)e.getComponent()).dispose();
					System.exit(0);
				}
			}
		);
	}
}

事件对象

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


public class Calc1 implements ActionListener{
	Frame f;
	TextField tf1;
	Button b1,b2,b3,b4;
	void display(){
		f=new Frame("Calculation");
		f.setSize(260,150);
		f.setLocation(320,240);
		f.setBackground(Color.lightGray);
		f.setLayout(new FlowLayout(FlowLayout.LEFT));//改变布局且左对齐
		tf1=new TextField(30);
		tf1.setEditable(false);
		f.add(tf1);
		b1=new Button("1");
		b2=new Button("2");
		b3=new Button("+");
		b4=new Button("C");
		f.add(b1);
		f.add(b2);
		f.add(b3);
		f.add(b4);
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		b4.addActionListener(this);
		f.addWindowStateListener(new WinClose());
		f.setVisible(true);
	}
	public void actionPerformed(ActionEvent e){
		if(e.getSource()==b4){
			tf1.setText("");
		}else{
			tf1.setText(tf1.getText()+e.getActionCommand());
		}
	}
	public static void main(String arg[]){
		(new Calc1()).display();
	}
}
class WinClose extends WindowAdapter{
	public void windowClosing(WindowEvent e){
		//覆盖windowAdaoter类中的同名方法
		System.exit(0);
	}
}

事件触发原理

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

class MyButton extends Button{
	MyButton(String s){
		super(s);
		enableEvents(AWTEvent.ACTION_EVENT_MASK);
	}
	protected void processActionEvent(ActionEvent e){
		System.out.println("按钮"+e.getActionCommand()+"按下!");
	}
};

public class TestEvent {
	public static void main(String args[]){
		Frame f=new Frame("TestEvent");
		MyButton b=new MyButton("login");
		b.setSize(1000,1000);
		f.add(b);
		f.setBackground(Color.lightGray);
		f.setVisible(true);
		f.pack();
		f.addWindowListener(new WindowAdapter(){  
            public void windowClosing(WindowEvent e){  
                System.out.println("我退出了!");  
                e.getWindow().setVisible(false);  
                ((Window)e.getComponent()).dispose();  
                System.exit(0);  
            }  
        }  
    );  
	}
}






阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页