Я пытаюсь отслеживать движения мыши, но получаю следующую ошибку: MouseTracker.Handlerclass не является абстрактным и не переопределяет абстрактный метод mouseExited (java.awt.event.MouseEvent) в java.awt. event.MouseListenerПереопределение абстрактных методов событий мыши
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);
}
}
Методы в интерфейсе (и в Java как общее правило) начинаются с строчной буквы. Java чувствительна к регистру, поэтому вы получаете не реализованную ошибку для вашего метода «MouseExited». –
Посмотрите на метод имя 'mouseExited' и ваши методы ...' MouseExited' увидеть разницу? – 3kings
@nbhuiya Ну, вам нужно реализовать этот метод из-за 'MouseMotionListener' – 3kings