java [25] 图形化 —— 事件处理

设计一个小程序,通过点击不同的按钮让面板盖面颜色:

代码:

package tank;

/*功能讲解事件处理机制
 * 
 * */


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;



public class test2 extends JFrame implements ActionListener{
	
	Mypanel mp;
	JButton jb1,jb2;
	Cat cat = new Cat();
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		test2 t1 = new test2();
	}
	
	public test2() {
		mp = new Mypanel();
		jb1 = new JButton("Black");
		jb2 = new JButton("Red");
		
		mp.setBackground(Color.blue);
		this.add(jb1,BorderLayout.NORTH);
		this.add(jb2,BorderLayout.SOUTH);
		this.add(mp);
		
		//注册监听:
		jb1.addActionListener(this);
		jb1.addActionListener(cat);
		
		//指定action命令
		jb1.setActionCommand("111");
		
		//注册监听:  可被监听多个。
		jb2.addActionListener(this);
		jb2.addActionListener(cat);
		
		//指定action命令
		jb2.setActionCommand("222");
			
		this.setSize(300,200);
		this.setLocation(400, 300);
		this.setTitle("event");
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.setVisible(true);
		
		
	}
	//对事件处理的方法  
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		//判断点击的是哪个按钮
		if (e.getActionCommand().equals("111")) {
			System.out.println("You put a black button");
			mp.setBackground(Color.BLACK);
			
		}else if(e.getActionCommand().equals("222")) {
			System.out.println("You put a red button");
			mp.setBackground(Color.red);
		} else {
			System.out.println("Action error");
		}
		
		
		
	}

}


class Mypanel extends JPanel{
	
	
	
	
}

class Cat implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getActionCommand().equals("111")) {
			System.out.println("Cat knows you put a black button");
		} else if(e.getActionCommand().equals("222")) {
			System.out.println("Cat knows you put a red button");
			
		}
	}
	
	
	
	
	
}



控制组件上下左右移动:

package tank;

/*功能:加深对事件处理的机制
 * 
 * 
 * 
通过上下左右控制组件的移动*/



import java.awt.*;
import javax.swing.*;
import java.awt.Event;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class test3 extends JFrame{
	panel mp;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		test3 test = new test3();
	}
	
	public test3() {
		mp = new panel();
		
		this.add(mp);
		this.addKeyListener(mp);
		
		this.setTitle("Event");
		this.setLocation(300, 100);
		this.setSize(300, 200);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.setVisible(true);
		
		
		
	}
	

}



//定义自己的panel
class panel extends JPanel implements KeyListener{
		int x =10;
		int y = 10;
	public void paint(Graphics g) {
		super.paint(g);
		g.setColor(Color.BLUE);
		g.fillOval(x, y, 30, 30);
		
		
	}
	
	// 键的一个值被输出  
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		//System.out.println("键被按下 !!" + e.getKeyChar());
		
	}
	
	//键被按下 
	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		//System.out.println("键被按下 !!" + e.getKeyChar());
		if (e.getKeyCode() == KeyEvent.VK_DOWN) {
			//System.out.println("|");
			y +=10;
		}else if (e.getKeyCode() == KeyEvent.VK_UP) {
			 y -=10;
		}else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			x -=10;
		} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			x +=10;
		}
		// 图形重绘;
		this.repaint();
	}
	
	//键被释放 
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
		
	
}

事件源

事件源是一个产生或者触发事件的对象,比如前面jbutton类的对象button1,当这个事件源的某些状态以某种方式发生改变时,就会产生某种类型的时间(一个事件源可能会产生多个不同类型的事件)。如果某个组件希望得到事件源产生的事件,就需要在这个时间上注册。

事件:

事件就是承载事件源状态改变时的信息对象,或者说,事件是事件源向事件监听器传输事件源监听状态信息的载体。当用户与GUI组件进行交互时就会生成事件,比如当鼠标在面板中移动时,就会生成一个鼠标移动事件的对象,而这个对象保存着当前鼠标在面板中的位置信息,java.awt.event包和javax.swing.event包中定义了各种事件类型。

事件类的类型:

事件监听接口

事件监听者实际上是一个类,该类实现了某个时间监听接口,前面的mypanel就是一个类,它实现了actionlistenr接口,它就可以作为一个事件监听者,对接收到的事件进行处理。

事件监听器接口有多种,不同的事件监听器接口可以监听不同的事件,一个类可以实现一个事件监听接口,也可以实现多个监听接口,这些接口在java.awt.event和javax.swing.event包中定义。

一个类实现实现多个监听接口案例:

package tank;


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class test4 extends JFrame {
	mypanel4 mp =null;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		test4 test = new test4();
	}
	public test4() {
		mp = new mypanel4();
		this.addMouseListener(mp);
		this.addKeyListener(mp);
		this.addMouseMotionListener(mp);
		this.addWindowListener(mp);
		
		this.setTitle("Event");
		this.setLocation(300, 100);
		this.setSize(300, 200);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.setVisible(true);
		
		
	}

}

//1.让mypanel 知道鼠标按下的消息,并且知道鼠标的位置(x,y); 
//2.让mypanel知道那个键按下的;
//3.让鼠标知道鼠标移动拖拽  MouseMotionListener
//让panel知道串口的变化关闭最小化最大化等 WindowListener

class mypanel4 extends JPanel implements MouseListener,KeyListener,MouseMotionListener,WindowListener{
	
	public void paint(Graphics g) {
		super.paint(g);
		
		
		
		
	}
	
	
	//1.鼠标点击
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.println("鼠标点击了x=" + e.getX() );
		System.out.println("鼠标点击了y=" + e.getY() );
	}
	//2.鼠标按下
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.println("摁着在");
	}
	//3.鼠标松开
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.println("松手了");
	}
	//4.鼠标进入panel
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.println("鼠标来了");
	}
	//5.鼠标离开
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.println("鼠标走了");
	}


	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		/*System.out.println(e.getKeyCode());
		System.out.println(e.getWhen());
		System.out.println(e.getKeyChar());
		System.out.println(e.getKeyLocation());
		System.out.println(e.getModifiersEx());
		System.out.println(e.getSource());*/
	}


	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e.getKeyChar());
	}


	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.println("鼠标当前坐标x:"+ e.getX());
		System.out.println("鼠标当前坐标y:"+ e.getY());
	}

	//窗口打开
	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		System.out.println("windowOpened");
	}

	//窗口正在关闭,
	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		System.out.println("windowClosing");
	}

	//窗口关闭了
	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		System.out.println("windowClosed");
	}

	//窗口最小化 到任务栏 
	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		System.out.println("windowIconified");
	}

	//窗口显示出来 
	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		System.out.println("windowDeiconified");
	}

	//窗口激活了窗口在最前面 
	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		System.out.println("windowActivated");
	}

	//窗口死了没有在使用 
	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		System.out.println("windowDeactivated");
	}
	
	
	
}

猜你喜欢

转载自blog.csdn.net/qq_38125626/article/details/81356013
今日推荐