用匿名类 来 做事件处理
代码如下:
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();
这句话 其他都不能实现
这是怎么回事啊???请老大 急救啊~~~ 不甚感激
代码如下:
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();
这句话 其他都不能实现
这是怎么回事啊???请老大 急救啊~~~ 不甚感激
我把代码改了一下~顺便也说一下~
关闭窗口调用的是windowClosing,不是windowClosed
关闭是System.exit(0);,不是System.exit(1);
public void mouseEntered()方法是需要参数的
正确的是
public void mouseEntered(MouseEvent e)
public void mouseExited(MouseEvent e)顺便接分
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();
}
}