菜鸟提问

llhbtt 2006-02-08 05:05:23
用匿名类 来 做事件处理
代码如下:
public class Three
{
private Frame f;
private TextField tf;

public void go()
{
f = new Frame("It is Inner class");
tf= new TextField(30);

f.add(tf,"South");

f.addMouseListener(new MouseAdapter(){
public void mouseEntered()
{
String s="Look,the mouse is coming!";
tf.setText(s);
}
public void mouseExited()
{
String s="Oh~~~,the mouse is left";
tf.setText(s);
}
} );

//注册监听器MouseListener
f.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e)
{
String s="The mouse dragging: x="+e.getX()+"y="+e.getY();
tf.setText(s);
}
});
//注册监听器WindowListener
f.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e)
{
System.exit(1);
}
});

f.setSize(400,400);
f.setVisible(true);
}


public static void main(String rags[])
{
Three t = new Three();
t.go();
}
}
但是只能实现 String s="The mouse dragging: x="+e.getX()+"y="+e.getY();
这句话 其他都不能实现
这是怎么回事啊???请老大 急救啊~~~ 不甚感激
...全文
72 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jlbqd 2006-02-11
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
public class Three
{
private Frame f;
private TextField tf;

public void go()
{
f = new Frame("It is Inner class");
tf= new TextField(30);

f.add(tf,"South");

f.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e)
{
String s="Look,the mouse is coming!";
tf.setText(s);
}
public void mouseExited(MouseEvent e)
{
String s="Oh~~~,the mouse is left";
tf.setText(s);
}
} );

//注册监听器MouseListener
f.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e)
{
String s="The mouse dragging: x="+e.getX()+"y="+e.getY();
tf.setText(s);
}
});
//注册监听器WindowListener
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

f.setSize(400,400);
f.setVisible(true);
}


public static void main(String rags[])
{
Three t = new Three();
t.go();
}
}
jlbqd 2006-02-11
  • 打赏
  • 举报
回复
问题不大~
我把代码改了一下~顺便也说一下~
关闭窗口调用的是windowClosing,不是windowClosed
关闭是System.exit(0);,不是System.exit(1);
public void mouseEntered()方法是需要参数的
正确的是
public void mouseEntered(MouseEvent e)
public void mouseExited(MouseEvent e)

顺便接分

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧