事件处理

事件处理:

事件处理的组成:

事件源:发生事件的GUI部件

事件:用户对事件源进行操作触发事件

事件监听者:负责对事件进行处理

事件处理流程:

1.注册监听者:btn.addActionListener(监听者)

2.监听者要继承相应的接口,实现接口中相应的方法:public  void actionPerFormed(ActionEvent e){.....}

//示例在AWT写的窗口中添加关闭窗口的监听器,注意如果是在Swing下这个就不用写了,毕竟人家都已经实现这个功能了,其实在生活中很少用到AWT插件了,不过事件监听思想还是要练习的

package com.sunchongwei;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Windowlistener extends Frame {
public Windowlistener() {
	super("测试窗口关闭功能");
	this.addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent e) {
			System.exit(0);//程序退出
		}
	});
	int width=200,height=200;
	this.setSize(width, height);
	this.setVisible(true);
}
public static void main(String[] args) {
	new Windowlistener();
}
}

描述信息接口名称方法
单机按钮、菜单项、文本框及按回车等动作ActionListeneractionPerformer(ActionEvent e)
选择了可选择的项目ItemListeneritemStateChanged(ItemEvent e)
文本部件内容的改变TextListenertextValueChanged(TextEvent e)
移动了滚动条等操作AdjustmentListeneradjustmendChanged(AdjustmentEvent e)
鼠标移动MouseListener 
键盘输入KeyListener 
部件失去或收到焦点FocusListener 

部件移动、缩放、显示、/隐藏等

ComponentListener 
窗口事件WindowListener

windowClosing(WindowEvent e)

windowOpened(WindowEvent e)

容器增加删除部件ContainerListener 

在事件处理中添加事件源

一个事件可以由多个监听者,一个监听者也可以监听多个事件源,采用以下方法区分事件源

~.getSource();//用来获取事件源对象的引用。

//附录一个模拟计算的小程序,主要用来讲事件监听

package com.sunchongwei;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Windowlistener extends Frame implements ActionListener{
TextField resultab;
Button jia,jian,cheng,chu,guiling;
Button get;
double a=0;//用来存储上一阶段的数字
int tar=0;
public Windowlistener() {
	super("制作一个简单的计算器示例");
	this.setLayout(new FlowLayout());//设置为流式布局
	resultab=new TextField(4);
	guiling=new Button("归零");
	guiling.addActionListener(this);
	jia=new Button("+");
	jia.addActionListener(this);
	get=new Button("=");
	get.addActionListener(this);
	jian=new Button("-");
	jian.addActionListener(this);
	cheng=new Button("*");
	cheng.addActionListener(this);
	chu=new Button("/");
	chu.addActionListener(this);
	this.add(resultab);
	this.add(get);
	this.add(jia);
	this.add(jian);
	this.add(cheng);
	this.add(chu);
	this.add(guiling);
	this.setSize(300, 300);
	this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
	if(e.getSource()==guiling) {
		resultab.setText("0");
	}
	if(e.getSource()==jia) {
		a=Double.parseDouble(resultab.getText());
		tar=1;
		resultab.setText(" ");
	}
	if(e.getSource()==jian) {
		a=Double.parseDouble(resultab.getText());
		tar=2;
		resultab.setText(" ");
	}
	if(e.getSource()==cheng) {
		a=Double.parseDouble(resultab.getText());
		tar=3;
		resultab.setText(" ");
	}
	if(e.getSource()==chu) {
		a=Double.parseDouble(resultab.getText());
		tar=4;
		resultab.setText(" ");
	}
	if(e.getSource()==get) {
		double b=Double.parseDouble(resultab.getText());
		switch(tar) {
		case 1:a+=b;break;
		case 2:a-=b;break;
		case 3:a*=b;break;
		case 4:a/=b;break;
		}
		resultab.setText(String.valueOf(a));
	}
}
public static void main(String[] args) {
	Windowlistener window=new Windowlistener();
	window.addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	});
	
}
}










转载于:https://www.cnblogs.com/sunchongwei/p/9567654.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值