import java.awt.*;
import java.awt.event.*;
public class TwoListenInner {
private Frame f;
private TextField tf;
public static void main(String args[]) {
TwoListenInner that = new TwoListenInner();
that.go();
}
public void go() {
f = new Frame("Two listeners example");
f.add("North", new Label("Click and drage the mouse"));
tf = new TextField(30);
f.add("South", tf);
f.addMouseMotionListener(new MouseMotionHandler());
f.addMouseMotionListener(new MouseEventHandler());
f.setSize(300, 300);
f.setVisible(true);
}
public class MouseMotionHandler extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
String s = "MouseDragging:X=" + e.getX() + " Y=" + e.getY();
tf.setText(s);
}
}
public class MouseEventHandler extends MouseMotionAdapter {
public void mouseEntered(MouseEvent e) {
String s = "The mouse entered";
tf.setText(s);
}
public void mouseExited(MouseEvent e){
String s="The mouse left the building";
tf.setText(s);}
}
}
我希望达到的效果是:当鼠标进入Frame时文本框提示“The mouse entered”,鼠标在Frame中拖动时显示“MouseDragging:X= Y= ”,鼠标离开时显示“The mouse left the building”.
谁帮我看看是在错在哪里啊?偶是个初学者。。。
----------------解决方案--------------------------------------------------------
同时注册两个MouseMotionListener?..没见过.
----------------解决方案--------------------------------------------------------
第二个其实是MouseListener,但是编译时出现错误。Eclipse提示修改成了MouseMotionListener.
----------------解决方案--------------------------------------------------------
MouseListener的mouseEntered和mouseExited两个方法
----------------解决方案--------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
public class TwoListenInner {
private Frame f;
private TextField tf;
public static void main(String args[]) {
TwoListenInner that = new TwoListenInner();
that.go();
}
public void go() {
f = new Frame(\"Two listeners example\");
f.add(\"North\", new Label(\"Click and drage the mouse\"));
tf = new TextField(30);
f.add(\"South\", tf);
f.addMouseMotionListener(new MouseMotionHandler());
f.addMouseListener(new MouseEventHandler());
f.setSize(300, 300);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setVisible(true);
}
public class MouseMotionHandler extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
String s = \"MouseDragging:X=\" + e.getX() + \" Y=\" + e.getY();
tf.setText(s);
}
}
public class MouseEventHandler extends MouseAdapter { //注意这儿
public void mouseEntered(MouseEvent e) {
String s = \"The mouse entered\";
tf.setText(s);
}
public void mouseExited(MouseEvent e){
String s=\"The mouse left the building\";
tf.setText(s);}
}
}
----------------解决方案--------------------------------------------------------
/**
* @(#)MyTest.java
*
*
* @author if
* @version 1.00 2007/3/30
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyTest extends JPanel implements MouseListener{
private JTextField jtf;
public MyTest(){
jtf=new JTextField(15);
add(jtf);
addMouseListener(this);
}
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
jtf.setText("enter");
}
public void mouseExited(MouseEvent e){
jtf.setText("exit");
}
public void mouseReleased(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public static void main(String[] args){
JFrame frame=new JFrame("mouseListener Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyTest());
frame.setSize(400,400);
frame.setVisible(true);
}
}
----------------解决方案--------------------------------------------------------
被快一步了.
----------------解决方案--------------------------------------------------------
我改了之后还是达不到效果啊
----------------解决方案--------------------------------------------------------
6楼的朋友,我那个例子是书上写的如何使用内部类的一段代码。你写的那个和我想了解的有区别啊
----------------解决方案--------------------------------------------------------
你要了解内部类.也是一样的.
只是将implements MouseListener和其方法实现写在一个内部类里就行.
----------------解决方案--------------------------------------------------------