Swing-事件处理机制

/**
 * java事件处理机制深入理解
 * 一个事件源挂载多个监视器
 */
package cn.jmy.test4;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ListenerDemo extends JFrame{
	MyPanel mp=null;
	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
		
		
		ListenerDemo ld=new ListenerDemo();
		
	}
	ListenerDemo(){
		mp=new MyPanel();
		
		this.add(mp);
		this.addKeyListener(mp);
		this.addMouseListener(mp);
		this.addWindowListener(mp);
		
		this.setSize(400,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
	}

}
class MyPanel extends JPanel implements KeyListener,MouseListener,WindowListener{

	private static final long serialVersionUID = 1L;

	public void paint(Graphics g){
		super.paint(g);
	}

	@Override
	public void keyTyped(KeyEvent e) {
	}

	@Override
	public void keyPressed(KeyEvent e) {
		System.out.println(e.getKeyChar()+"键被按下");
	}

	@Override
	public void keyReleased(KeyEvent e) {
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		System.out.println("mouseClicked"+e.getX()+","+e.getY());
	}

	@Override
	public void mousePressed(MouseEvent e) {
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		System.out.println("mouseEntered");
	}

	@Override
	public void mouseExited(MouseEvent e) {
		System.out.println("mouseExited");
	}

	@Override
	public void windowOpened(WindowEvent e) {
		System.out.println("windowOpened");
	}

	@Override
	public void windowClosing(WindowEvent e) {
		System.out.println("windowClosing");
	}

	@Override
	public void windowClosed(WindowEvent e) {
		System.out.println("windowClosed");
	}

	@Override
	public void windowIconified(WindowEvent e) {
		System.out.println("windowIconified");
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		System.out.println("windowDeiconified");
	}

	@Override
	public void windowActivated(WindowEvent e) {
		System.out.println("windowActivated");
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		System.out.println("windowDeactivated");
	}

	
	
}


你可能感兴趣的:(一个事件源挂载多个监视器)