第一次发求救贴,求大家多帮帮忙
想做一个窗口,鼠标移动到上面会触发事情
代码:
import java.awt.*;
import java.awt.event.*;
public class Demo3{
private Frame f;
private TextField tf;
public static void main(String args []){
Demo3 that=new Demo3();
this.go();
}
public void go(){
f=new Frame("two listeners example");
f.add("North",new Label("Click and drag the nouse"));
tf=new TextField(30);
f.add("South",tf);
f.addMouseMotionListener(new MouseMotionHandler());
f.addMouseListener(new MouseEventHandler());
f.setVisible(true);
}
public class MouseMotionHandler extends MouseMotionAdapter{
public void mouseDragged(MouseEvent e){
String s="Mouse dragging: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);
}
}
}
想做一个窗口,鼠标移动到上面会触发事情
代码:
import java.awt.*;
import java.awt.event.*;
public class Demo3{
private Frame f;
private TextField tf;
public static void main(String args []){
Demo3 that=new Demo3();
this.go();
}
public void go(){
f=new Frame("two listeners example");
f.add("North",new Label("Click and drag the nouse"));
tf=new TextField(30);
f.add("South",tf);
f.addMouseMotionListener(new MouseMotionHandler());
f.addMouseListener(new MouseEventHandler());
f.setVisible(true);
}
public class MouseMotionHandler extends MouseMotionAdapter{
public void mouseDragged(MouseEvent e){
String s="Mouse dragging: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);
}
}
}
Demo3 that=new Demo3();
this.go();//that.go()
}
this表示当前对象,static的方法可以直接用 类.方法名来 访问,这样就没有对象。
所以不能这样用咯声明,本人新手
import java.awt.*;
import java.awt.event.*;public class Demo3 {
private Frame f; private TextField tf; public void go() {
f = new Frame("two listeners example");
f.add("North", new Label("Click and drag the nouse"));
tf = new TextField(30);
f.add("South", tf);
f.addMouseMotionListener(new MouseMotionHandler());
f.addMouseListener(new MouseEventHandler()); f.setVisible(true);
} public class MouseMotionHandler extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
String s = "Mouse dragging: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);
}
}
}//////////////////////////////////////
public class Test { public static void main(String[] args) {
Demo3 that = new Demo3();
that.go(); }}
但是楼主这个错误,把that打成this确实让我也湿了。