java的鼠标事件
副标题#e#
MouseListener 要领接口中的mouseClicked() 要领和mousePressed() 要领有什么区别
这是一个热门的话题,让我们来深入的找寻谜底,Java’s AWT 库提供了两个接口来监听和吸收鼠标事件. 一个是 java.awt.event.MouseListener:
public interface MouseListener extends EventListener
{
public void mousePressed(MouseEvent e);
public void mouseReleased(MouseEvent e);
public void mouseClicked(MouseEvent e);
public void mouseEntered(MouseEvent e);
public void mouseExited(MouseEvent e);
}
另一个是 java.awt.event.MouseMotionListener:
public interface MouseMotionListener extends EventListener
{
public void mouseDragged(MouseEvent e);
public void mouseMoved(MouseEvent e);
}
Swing 提供了 MouseInputListener, 这个接口的同时扩展了MouseMotionListener 和MouseListener 接口.
让我们来看看这些接口中的要领:
mousePressed() 当用户按下鼠标按钮时产生.
mouseReleased() 当用户松开鼠标按钮时产生.
mouseClicked() 当用户按下并松开鼠标按钮时产生. 用户在选择或双击图标的时候凡是会点击鼠标按钮. 用户假如在松开鼠标之前移动鼠标,点击不会导致鼠标相应事件呈现.
因为点击鼠标是按下鼠标和松开鼠标的团结, 在事件分派给 mouseClicked() 要领之前, mousePressed() 和 mouseReleased() 要领已同时被挪用.
mouseEntered() 当鼠标分开当前组件并进入你所监听的组件时激活事件.
mouseExited() 当鼠标分开你所监听的组件时产生.
mouseDragged() 当用户按下鼠标按钮并在松开之前举办移动时产生.在mouseDragged() 后松开鼠标不会导致mouseClicked().
mouseMoved() 当鼠标在组件上移动而 不时拖动时产生.
要监听鼠标事件就必需挪用这些接口之一,或扩展一个鼠标适配器(mouse adapters) 类. AWT 提供了两种监听适配器(listener adapters): java.awt.event.MouseAdapter 和java.awt.event.MouseMotionAdapter.
Swing 为MouseInputListener提供了一个叫做javax.swing.event.MouseInputAdapter的适配器. 有了适配器你就不必挪用接口中每个要领了. 取而代之, 你只需简朴的扩展适配器并重写你要监听的要领就可以了.
#p#副标题#e#
假如你照旧很疑惑, 再看看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 举办试验的好要领.