如果你还是很迷惑, 再看看API 并写个小测试程序. 有时尝试理解不同事件的最好方法different events. 我写了一个小小的应用程序来表现两种鼠标事件的区别.这里是完整的代码:
import java.awt.*;
import java.awt.event.*;
public class MouseTest extends Frame
{
public MouseTest()
{
Button b = new Button("JavaWorld JavaQ&A");
b.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
);
add(b,BorderLayout.NORTH);
addMouseListener(new MouseTest.MouseHandler());
addMouseMotionListener(new MouseTest.MouseMotionHandler());
}
// MouseHandler is an inner class that implements the MouseListener.
// Each method simply prints out a message to the command line.
private class MouseHandler implements MouseListener
{
public void mousePressed(MouseEvent e)
{
System.out.println("mouse pressed");
}
public void mouseClicked(MouseEvent e)
{
System.out.println("moused clicked");
}
public void mouseReleased(MouseEvent e)
{
System.out.println("mouse released");
}
public void mouseEntered(MouseEvent e)
{
System.out.println("mouse entered");
}
public void mouseExited(MouseEvent e)
{
System.out.println("mouse exited");
}
}
// MouseMotionHandler is an inner class that implements the MouseMotionListener.
// Each method simply prints out a message to the command line.
private class MouseMotionHandler implements MouseMotionListener
{
public void mouseMoved(MouseEvent e)
{
System.out.println("mouse moved");
}
public void mouseDragged(MouseEvent e)
{
System.out.println("mouse dragged");
}
}
public static void main(String[] args)
{
new MouseTest().show();
}
} |
这个应用程序建立了一个窗口和一个按钮.当你移动鼠标并按下按钮时, 应用程序就会在命令行打印出文本消息, 让你看到鼠标响应和事件间的对应关系.
象上边那样写一个小小的测试程序是对陌生的API 进行试验的好方法. |