java一类多监听小案例

package com.test2;

import com.test1.MyTankGame1;

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

public class implementTest extends JFrame{
    MyPanel mp = null;
    public static void main(String [] args)
    {
        implementTest test = new implementTest();
    }

    public implementTest()
    {
        mp = new MyPanel();
        this.add(mp);

        this.addMouseListener(mp);
        this.addMouseMotionListener(mp);
        this.addWindowListener(mp);
        this.setSize(400,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
}

class MyPanel extends JPanel implements MouseListener,MouseMotionListener,WindowListener
{
    public void paint(Graphics g)
    {
        super.paint(g);
    }


    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("鼠标被按下,x = " + e.getX() + ", y = " + e.getY());
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

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

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

    @Override
    public void mouseDragged(MouseEvent e) {
        //System.out.println("正在拖拽!");
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        //System.out.println("鼠标正在移动");
    }

    @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");
    }
}

 

你可能感兴趣的:(java_demo,java)