2016-03-29 13 views
0

Ben fare hareketlerini izlemek için çalışıyorum, ancak aşağıdaki hatayı alıyorum: MouseTracker.Handlerclass soyut değildir ve java.awt soyut yöntem mouseExited (java.awt.event.MouseEvent) geçersiz kılmaz. event.MouseListeneroveriding soyut yöntem fare olayları

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); 
    } 
} 
+1

Arabirimdeki (ve genel bir kural olarak Java'da) yöntemler, küçük harfle başlar. Java büyük/küçük harfe duyarlıdır, dolayısıyla 'MouseExited 'yönteminiz için uygulanmayan bir hata alırsınız. –

+0

'mouseExited' yöntemine ve yönteminize bakın ... 'MouseExited' farkı görüyor musunuz? – 3kings

+1

@nbhuiya Peki, bu yöntemi "MouseMotionListener" – 3kings

cevap

3

MouseListener geçerli:

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); 

}

Yani

Eğer yöntemlerde yanlış bir durum var ... Onlar lowe ile başlamalıdır r durum.

+1

Bu ayrıca '' '@ Override'' kullanmanın iyi bir fikir olmasının sebebidir. Bu, bir yöntemi geçersiz kılan bir _actually_ yapar. –