2016-08-25 181 views
0

我寫了一些代碼在java中創建一個簡單的鼠標事件。但我堅持這一點。有人可以請幫忙弄清楚這是什麼問題或者可能是我的代碼中缺少的東西。鼠標事件處理Java

package mouseeventshandling; 

import java.awt.*; 
import java.awt.event.*; 
public class MouseEventsHandling extends Frame implements MouseListener, MouseMotionListener { 
    TextField tf; 
    public MouseEventsHandling(String title){ 
     super(title); 
     tf=new TextField(60); 
     addMouseListener(this); 
    } 
    public void launchFrame(){ 
     add(tf, BorderLayout.SOUTH); 
     setSize(300,300); 
     setVisible(true); 
    } 
    public void mouseClicked(MouseEvent me){ 
     String msg = "Mouse Clicked"; 
     tf.setText(msg); 
    } 
    public void mouseEntered(MouseEvent me){ 
     String msg = "Mouse Entered Component"; 
     tf.setText(msg); 
    } 
    public void mouseExited(MouseEvent me){ 
     String msg = "Mouse Exited Component"' 
     tf.setText(msg); 
    } 
    public void mousePressed(MouseEvent me){ 
     String msg = "Mouse pressed"; 
     tf.setText(msg); 
    } 
    public void mouseReleased(MouseEvent me){ 
     String msg = "Mouse released"; 
     tf.setText(msg); 
    } 
    public void mouseDragged(MouseEvent me){ 
     String msg = "Mouse dragged at " + me.getX() + "," + me.getY(); 
     tf.setText(msg); 
    } 
} 
+0

那麼問題是什麼? – richersoon

回答

0

由於您正在實現接口,所以需要實現所有功能。你缺少mouseMoved

package mouseeventshandling; 
import java.awt.*; 
import java.awt.event.*; 
public class MouseEventsHandling extends Frame implements MouseListener, MouseMotionListener { 
    TextField tf; 
    public MouseEventsHandling(String title){ 
     super(title); 
     tf=new TextField(60); 
     addMouseListener(this); 
    } 
    public void launchFrame(){ 
     add(tf, BorderLayout.SOUTH); 
     setSize(300,300); 
     setVisible(true); 
    } 
    public void mouseClicked(MouseEvent me){ 
     String msg = "Mouse Clicked"; 
     tf.setText(msg); 
    } 
    public void mouseEntered(MouseEvent me){ 
     String msg = "Mouse Entered Component"; 
     tf.setText(msg); 
    } 
    public void mouseExited(MouseEvent me){ 
     String msg = "Mouse Exited Component"; 
     tf.setText(msg); 
    } 
    public void mousePressed(MouseEvent me){ 
     String msg = "Mouse pressed"; 
     tf.setText(msg); 
    } 
    public void mouseReleased(MouseEvent me){ 
     String msg = "Mouse released"; 
     tf.setText(msg); 
    } 
    public void mouseDragged(MouseEvent me){ 
     String msg = "Mouse dragged at " + me.getX() + "," + me.getY(); 
     tf.setText(msg); 
    } 
    @Override 
    public void mouseMoved(MouseEvent arg0) { 

    } 
} 

我建議使用一些IDE像Eclipse或NetBeans的,這將使它更容易些。