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);  
            }  
        }  
    );  
	}
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值