• Java事件基础


    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);
        }
        
    
    }
  • 相关阅读:
    原创 C++应用程序在Windows下的编译、链接(四)动态链接
    IE浏览器 json异常
    Linux系统github使用
    Mysql in 排序
    转 php四种基础算法:冒泡,选择,插入和快速排序法
    转 mysql取今天,明天,工作日,周末,本周,下周,下月数据
    下载远程图片到本地
    转 PHP中SQL_CALC_FOUND_ROWS与FOUND_ROWS()和count()
    星级点评
    21个值得收藏的Javascript技巧
  • 原文地址:https://www.cnblogs.com/douzujun/p/6764901.html
Copyright © 2020-2023  润新知