Lesson_20_Note_swing事件处理

package Lesson_20;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;

/**************************************************************
 * 							2013.3.19
 * 				 	         Swing事件
 * 					  	   @author Gavin
 **************************************************************/
/******************************课堂内容********************************
 * 1、用户与GUI组件进行交互就会发生事件,如:按下一个按钮、用键盘输入一个字符、点击鼠标等等
 * 2、Java中,事件处理的基本思路如下
 * 		一个源(事件源)产生一个事件(事件对象)并把它送到监听器那里,监听器只是简单地等待,
 * 		直到它收到一个事件,一旦事件被接受,监听器将处理这些事件
 * 3、一个事件源必须注册监听器以便监听器可以接受关于一个特定事件的通知
 * 4、事件源一定是指某个组件!当在一个图形用户界面点击鼠标或者按下键盘时,都是针对于具体组件而发生的动作
 * 5、监听器由一系列接口来提供,事件监听器就是实现了事件监听接口的类,监听器不断监听事件源的动作,当事件
 * 		源产生一个事件后,监听器接收到事件源的通知,就调用特定的方法,以执行指定的动作
 * 6、一般采用内部类的方式来实现监听器,内部类可以很方便地访问外部类中的其它成员
 * 		内部类实际上是外部类的成员,如果在外部类以外需要实例化内部类对象的话,则必须通过外部类的实例才可以
 * *7、要进行事件处理,除了使用内部类的方式以外,还可以:
 * 		--->直接实现的方式
 * 		--->匿名类的实现方式
 * 8、匿名类其实就是一种比较特殊的内部类,只是这个类没有名字而已
 *
 ************************************************************************/
public class EventFrame extends JFrame{
	
	private JButton btnOk;
	private JButton btnExit;
	private JButton btnChange1;
	private JButton btnChange2;
	public EventFrame() {
		//构造方法
		super("事件处理程序");
		this.setLayout(new FlowLayout());//布局(顺序布局)
		this.setSize(400, 300);
		initCompont();//调用初始化方法
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
	
	public void initCompont(){
		//初始化组件
		btnExit=new JButton("退出程序");
		btnExit.setActionCommand("exit");
		btnExit.setFocusPainted(false);//去掉点击按钮时产生的焦点框
		btnExit.addActionListener(new actionButton());//为按钮添加活动监听器,监听按钮的点击事件
		btnOk=new JButton("登录");				//参数是一个接口对象,故要写一个类实现ActionListener接口
		btnOk.setActionCommand("OK");//设置动作事件返回的命令字符内容
		btnOk.setFocusPainted(false);
		btnOk.addActionListener(new actionButton());//用匿名对象来实现
		
		btnChange1=new JButton("改变背景颜色");
		btnChange1.addActionListener(new actionListener3());
		
		btnChange2=new JButton("进入改变背景颜色");
		btnChange2.addMouseListener(new mouse());
		
//		this.getContentPane().addMouseMotionListener(new mouse1());//课堂练习,为面板添加监听
		this.getContentPane().addMouseMotionListener(new MouseMotionListener() {//匿名类写法
			
			public void mouseMoved(MouseEvent e) {//参数e包含了产生事件的对象和鼠标信息
				EventFrame.this.setTitle("事件处理程序"+"("+e.getX()+","+e.getY()+")");
			}
			
			public void mouseDragged(MouseEvent arg0) {
				System.out.println("拖动鼠标");
			}
		});
		
		
		this.add(btnChange2);
		this.add(btnChange1);
		this.add(btnExit);//添加到窗口
		this.add(btnOk);
	}
	
	class actionButton implements ActionListener{//内部类实现方法:在EventFrame类中定义了一个类
											//编写类来实现监听器接口,并重写其中的抽象方法
		//事件处理程序
		public void actionPerformed(ActionEvent e) {//系统会自动调用该方法
			//只需在该方法中加入要实现的功能即可
			if(e.getSource()==btnExit){//根据产生事件的源来区别是哪个按钮被点击
				//返回当前发生事件的对象
				System.out.println("退出按钮被点击!");
				System.exit(0);//退出程序
			}else if(e.getSource()==btnOk){
				System.out.println("OK按钮被点击!");
			}//一般情况下,不可能为每一个按钮都写一个类,故在这里做一个判断就好了
		}
	}
	
	class actionButton2 implements ActionListener{
		//还可以很据按钮返回的命令字符内容来进行判断
		public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand().equals("OK")){//对事件源返回的字符串进行判断
				System.out.println("Ok按钮被点击!");
			}else if(e.getActionCommand().equals("exit")){
				System.exit(0);
			}
		}
	}
	class actionListener3 implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==btnChange1){//如果按钮是改变背景颜色的按钮的话
				Random r=new Random();
				EventFrame.this.getRootPane().getContentPane().setBackground
							(new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)));//随机颜色
			}//RootPane是最外面的面板
		}
		
	}
	 
	class mouse implements MouseListener{//鼠标监听类

		public void mouseClicked(MouseEvent e) {
			
		}

		public void mouseEntered(MouseEvent e) {//进入,即当鼠标在进入按钮但并不点击时,改变颜色
			Random r=new Random();
			EventFrame.this.getRootPane().getContentPane().setBackground
						(new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)));//随机颜色
		}
		//这三个重写的方法没有用到,但也要写所有的方法,因为具有抽象方法的类不能实例化
		public void mouseExited(MouseEvent arg0) {
			
		}

		public void mousePressed(MouseEvent arg0) {
			
		}

		public void mouseReleased(MouseEvent arg0) {
			
		}
		
	}
	
	class mouse1 implements MouseMotionListener{

		public void mouseDragged(MouseEvent e) {
			
		}

		public void mouseMoved(MouseEvent e) {
			EventFrame.this.setTitle("事件处理程序"+"("+e.getX()+","+e.getY()+")");//获得坐标并在标题显示
		}
		
	}
	
	public static void main(String[] args) {
		EventFrame frame=new EventFrame();
	}
}
//事件的处理是由三个部分来实现的:(事件源.addXxxListener(监听器实例))
//1、确定事件源(组件):产生事件的对象
//2、注册事件类型监听:发生的事件的类型 --->void addXxxListener(XxxListener listener)
//3、事件处理程序(监听中的某个方法):(监听中的某个方法)

//课堂练习:鼠标在面板上移动,在标题上显示鼠标的坐标


执行图片:

Lesson_20_Note_swing事件处理_第1张图片Lesson_20_Note_swing事件处理_第2张图片

package Lesson_20;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class TextEvent extends JFrame implements ActionListener, KeyListener {
									//直接实现的方式来监听
	JButton  btnOk=new JButton("Ok");
	int x1;
	int y1;
	public TextEvent() {
		//构造方法
				super("事件处理程序");
				this.setLayout(new FlowLayout());//布局(顺序布局)
				this.setSize(400, 300);
				btnOk.addActionListener(this);//当前类的对象
				this.add(btnOk);
//				btnOk.setEnabled(false);//设置按钮不可用,将焦点移到框架上,这样窗体才能读到键盘的值
				btnOk.setFocusable(false);//让按钮失去焦点也行
				x1=btnOk.getLocation().x;
				y1=btnOk.getLocation().y;
//				this.add(new JButton("另一按钮"));//演示焦点
				this.addKeyListener(this);
				this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				this.setLocationRelativeTo(null);
				this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent ae) {//重写ActionListener接口里的抽象方法
		System.out.println("点击Ok按钮");
	}
	
	
	public void keyPressed(KeyEvent e) { //重写KeyListener接口里的抽象方法,下面两个没用到
		if(e.getKeyCode()==KeyEvent.VK_W){//即按下W键
			//vk表示虚拟键盘
			btnOk.setLocation(x1, y1-=16);//按钮向上移动16个单位
			if(y1<=0){		//当超过边框时,从下方进来,类似于贪吃蛇里
				y1=278;
			}
		}else if(e.getKeyCode()==KeyEvent.VK_S){//按下S键
			btnOk.setLocation(x1, y1+=16);		//按钮向下移动16个单位
			if(y1>=275){
				y1=0;
			}
		}else if(e.getKeyCode()==KeyEvent.VK_A){//按下A键
			btnOk.setLocation(x1-=16,y1);		//按钮向左移动16个单位
			if(x1<=0){					
				x1=380;
			}
		}else if(e.getKeyCode()==KeyEvent.VK_D){//按下D键
			btnOk.setLocation(x1+=16, y1);		//按钮向右移动16个单位
			if(x1>=380){
				x1=0;
			}
		}
	}

	public void keyReleased(KeyEvent e) {
		
	}
	

	public void keyTyped(KeyEvent e) {
	}
	
	public static void main(String[] args) {
		new TextEvent();
	}
}
//课堂练习:程序运行后,在窗体上按W 按钮往上移动,S往下 A往左 D往右

执行图片:

Lesson_20_Note_swing事件处理_第3张图片Lesson_20_Note_swing事件处理_第4张图片

你可能感兴趣的:(Lesson_20_Note_swing事件处理)