package cn.Douzi.Event;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Demo_moreListener extends JFrame {
Mypanel mp = null;
public Demo_moreListener() {
// TODO Auto-generated constructor stub
mp = new Mypanel();
this.add(mp);
//注册监听
this.addMouseListener(mp);
this.addKeyListener(mp);
this.addMouseMotionListener(mp);
this.addWindowListener(mp);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Demo_moreListener test = new Demo_moreListener();
test.setVisible(true);
}
}
//1.让Mypanel知道鼠标按下的消息,并且知道点击的位置(x,y)
//2.让Mypanel知道哪个键按下
//3.让MyPanel知道鼠标移动,拖拽
//4.让MyPanel知道窗口的变化:变化,最小化,最大化
class Mypanel extends JPanel implements WindowListener, MouseListener, KeyListener, MouseMotionListener {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
//窗口正在关闭
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("正在关闭........");
}
//窗口关闭
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
//窗口最小化
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
//窗口激活
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("窗口激活");
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("窗口最小化");
}
//鼠标移动
@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("鼠标移动");
}
//键事件
//键输入q w F1
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
//键按下,未松开
@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 mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("鼠标点击了 x = " + e.getX() + "y = " + e.getY());
}
//鼠标按下,但是还没有松开事件
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
//鼠标按下,然后 松开 的事件
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
//鼠标移动到Mypanel
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("鼠标来");
}
//鼠标离开Mypanel
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("鼠标走");
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
}
}