import java.awt.*; import java.awt.event.*; public class TwoListen implements MouseMotionListener, MouseListener { private Frame f; private TextField tf; public static void main(String args[]) { TwoListen two = new TwoListen(); two.go(); } public void go() { f = new Frame("Two listeners example"); f.add (new Label ("Click and drag the mouse"), "BorderLayout.NORTH"); tf = new TextField (30); f.add (tf);//, "BorderLayout.SOUTH"); f.addMouseMotionListener(this); f.addMouseListener (this); f.setSize(300, 200); f.setVisible(true); } // These are MouseMotionListener events public void mouseDragged (MouseEvent e) { String s = "Mouse dragging: X = " + e.getX() + " Y = " + e.getY(); tf.setText (s); } public void mouseMoved (MouseEvent e) { } // These are MouseListener events public void mouseClicked (MouseEvent e) { } public void mouseEntered (MouseEvent e) { String s = "The mouse entered"; tf.setText (s); } public void mouseExited (MouseEvent e) { String s = "The mouse has left the building"; tf.setText (s); } public void mousePressed (MouseEvent e) { } public void mouseReleased (MouseEvent e) { } }
把 f.add (new Label ("Click and drag the mouse"), "BorderLayout.NORTH"); 改为 f.add (new Label ("Click and drag the mouse"), "North");
import java.awt.event.*; public class TwoListen implements
MouseMotionListener, MouseListener {
private Frame f;
private TextField tf; public static void main(String args[]) {
TwoListen two = new TwoListen();
two.go();
}
public void go() {
f = new Frame("Two listeners example");
f.add (new Label ("Click and drag the mouse"), "BorderLayout.NORTH");
tf = new TextField (30);
f.add (tf);//, "BorderLayout.SOUTH"); f.addMouseMotionListener(this);
f.addMouseListener (this);
f.setSize(300, 200);
f.setVisible(true);
} // These are MouseMotionListener events
public void mouseDragged (MouseEvent e) {
String s =
"Mouse dragging: X = " + e.getX() + " Y = " + e.getY();
tf.setText (s);
}
public void mouseMoved (MouseEvent e) {
} // These are MouseListener events
public void mouseClicked (MouseEvent e) {
} public void mouseEntered (MouseEvent e) {
String s = "The mouse entered";
tf.setText (s);
}
public void mouseExited (MouseEvent e) {
String s = "The mouse has left the building";
tf.setText (s);
} public void mousePressed (MouseEvent e) {
} public void mouseReleased (MouseEvent e) {
}
}
f.add (new Label ("Click and drag the mouse"), "BorderLayout.NORTH");
改为
f.add (new Label ("Click and drag the mouse"), "North");