学习笔记二十五:事件处理(一)

人类要在竞争中求生存,更要奋斗。——孙中山


本讲内容:事件处理

导入包:

import java.awt.event.*;


一、事件分为两大类:低级事件和高级事件。

1、低级事件包括以下几种

事件类别接口名描述
ComponentEvent  组件事件ComponentListener发生于组件大小改变、移动、显示或隐藏时
ContainerEvent  容器事件ContainerListener发生于添加或删除一个组件
WindowEvent  窗口事件WindowListener窗口被激活、屏蔽、最小最 大化、或关闭
MouseEvent  鼠标事件MouseEventListener鼠标按键被按下、释放、鼠标移动或拖动
KeyEvent  键盘事件KeyListener键盘上的一个键被按下或释放
FocusEvent  焦点事件FocusEvent发生于组件获得焦点或者失去焦点时

2、高级事件包括以下几种

事件类别
接口名
描述
ActionEvent   动作事件ActionListener发生于按钮单击、菜单选择、列表框选择、在文本域中按“回车”等时
AdjustmentEvent 调整事件AdjustmentListener发生于用户在滚动条上移动滑块以调节数值时
ItemEvent 项目事件ItemListener发生于用户从一组选择框或者列表框中进行选择
TextEvent 文本事件TextListener发生于文本域或者文本框中的内容发生改变时

例一:两个按钮实现相应事件

public class Text extends JFrame implements ActionListener{
	JPanel mb=null;
	JButton an1,an2;
	
	public static void main(String[] args) {
		Text t=new Text();
	}
	
	public Text() {
		mb=new JPanel();
		mb.setBackground(Color.yellow);
		an1=new JButton("红色");
		an1.addActionListener(this);//注册监听
		an1.setActionCommand("rea");//指定action命令
		an2=new JButton("蓝色");
		an2.addActionListener(this);
		an2.setActionCommand("blue");
		
		this.add(an1,BorderLayout.NORTH);
		this.add(an2,BorderLayout.SOUTH);
		this.add(mb);
		
		//设置窗体属性
		this.setTitle("技术大牛—小劲");
		this.setLocation(300, 300);
		this.setSize(400,400);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("rea")){
			mb.setBackground(Color.red);
		}else if(e.getActionCommand().equals("blue")){
			mb.setBackground(Color.blue);
		}
	}
}

方法二(也可以获得监听事件)

public class Text extends JFrame {
	JPanel mb=null;
	JButton an1,an2;
	
	public static void main(String[] args) {
		Text t=new Text();
	}
	
	public Text() {
		mb=new JPanel();
		mb.setBackground(Color.yellow);
		an1=new JButton("红色");
		an2=new JButton("蓝色");
		MyListener listener=new MyListener();
		an1.addActionListener(listener);//注册监听
		an1.setActionCommand("red");//指定action命令
		an2.addActionListener(listener);
		an2.setActionCommand("blue");
		
		this.add(an1,BorderLayout.NORTH);
		this.add(an2,BorderLayout.SOUTH);
		this.add(mb);
		
		//设置窗体属性
		this.setTitle("技术大牛—小劲");
		this.setLocation(300, 300);
		this.setSize(400,400);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

class MyListener implements ActionListener{
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("red")){
			System.out.println("red");
		}else if(e.getActionCommand().equals("blue")){
			System.out.println("blue");
		}
	}
}



例二:一个长方形按键盘上下左右会动

public class Text extends JFrame {
	MyPanel mb=null;
	
	public static void main(String[] args) {
		Text t=new Text();//每定义一个 t 都会产生一个对应的this
	}
	
	public Text() {
		mb=new MyPanel();
		this.add(mb);
		this.addKeyListener(mb);//添加监听    键盘监听这个类所定义的对象  用mp对象进行监听
		
		//设置窗体属性
		this.setTitle("技术大牛—小劲");
		this.setLocation(300, 300);
		this.setSize(400,400);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

class MyPanel extends JPanel implements KeyListener{
	int x=100,y=100;
	public void paint(Graphics g) {
		super.paint(g);
		g.setColor(Color.red);
		g.fillRect(x, y, 10, 10);
	}
	
	//键被按下
	public void keyPressed(KeyEvent e) {
//		System.out.println("1");
		if(e.getKeyCode()==KeyEvent.VK_UP){
			y--;
		}else if(e.getKeyCode()==KeyEvent.VK_DOWN){
			y++;
		}else if(e.getKeyCode()==KeyEvent.VK_LEFT){
			x--;
		}else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
			x++;
		}
		//调用 repaint() 函数,来重绘制界面
		this.repaint();
	}
	
	//键被释放
	public void keyReleased(KeyEvent e) {
		System.out.println("键被释放");
	}
	
	<span style="color:#ff0000;">//键的一个值被打印输出(上下左右键等不会执行,因为没打印出)</span>
	public void keyTyped(KeyEvent e) {
		System.out.println("其它键会执行,上下左右键等不会执行,因为没打印出");
	}
}



本讲就到这里,Take your time and enjoy it
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值