简体   繁体   English

覆盖抽象方法鼠标事件

[英]Overiding abstract method mouse events

I am trying to track mouse movements, but am receiving the following error: MouseTracker.Handlerclass is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener 我正在尝试跟踪鼠标的移动,但是收到以下错误:MouseTracker.Handlerclass不是抽象的,并且不会覆盖java.awt.event.MouseListener中的抽象方法mouseExited(java.awt.event.MouseEvent)

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

public class MouseTracker extends JFrame{
    private JPanel board;
    private JLabel message;

    public MouseTracker(){
        super("Mouse Tracker");
        board = new JPanel();
        board.setBackground(Color.WHITE);
        add(board, BorderLayout.CENTER);

        message = new JLabel("No action preformed");
        add(message, BorderLayout.SOUTH);

        Handlerclass handler = new Handlerclass();
        board.addMouseListener(handler);
        board.addMouseMotionListener(handler);
    }

    private class Handlerclass implements MouseListener, MouseMotionListener{

        public void MousePressed(MouseEvent e){
            message.setText(String.format("The mouse was pressed, the current coordinates are: %d,%d", e.getX(), e.getY()));
            board.setBackground(Color.RED);
        }

        public void MouseClicked(MouseEvent e){
            message.setText(String.format("The mouse was clicked, the current coordinates are: %d,%d", e.getX(), e.getY()));
            board.setBackground(Color.BLUE);
        }

        public void MouseReleased(MouseEvent e){
            message.setText(String.format("The mouse was released, the current coordinates are: %d,%d", e.getX(), e.getY()));
            board.setBackground(Color.GREEN);
        }

        public void MouseEntered(MouseEvent e){
            message.setText(String.format("The mouse has entered the board, the current coordinates are: %d,%d", e.getX(), e.getY()));
            board.setBackground(Color.ORANGE);
        }

        public void MouseExited(MouseEvent e){
            message.setText(String.format("The mouse has exited the board, the current coordinates are: %d,%d", e.getX(), e.getY()));
            board.setBackground(Color.YELLOW);
        }

        public void MouseDragged(MouseEvent e){
            message.setText(String.format("The mouse was dragged, the current coordinates are: %d,%d", e.getX(), e.getY()));
           board.setBackground(Color.PINK);
        }
    }

    public static void main (String[]args){
        MouseTracker run = new MouseTracker();
        run.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        run.setSize(400,400);
        run.setVisible(true);
    }
}

MouseListener is: MouseListener是:

public interface MouseListener extends EventListener {

/**
 * Invoked when the mouse button has been clicked (pressed
 * and released) on a component.
 */
public void mouseClicked(MouseEvent e);

/**
 * Invoked when a mouse button has been pressed on a component.
 */
public void mousePressed(MouseEvent e);

/**
 * Invoked when a mouse button has been released on a component.
 */
public void mouseReleased(MouseEvent e);

/**
 * Invoked when the mouse enters a component.
 */
public void mouseEntered(MouseEvent e);

/**
 * Invoked when the mouse exits a component.
 */
public void mouseExited(MouseEvent e);

} }

So you got the case wrong in your methods... They should start with lower case. 因此,您在方法中遇到了错误的情况……它们应该以小写开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM