我有一个JFrame,其中包含许多具有不同功能的子组件和子面板。我已经为其中一些添加了mouseListeners。
我希望能够在框架中单击鼠标按钮时调用函数,而不管焦点是什么组件。我不想将mouselisteners添加到所有组件中。就像keyEventDispacher一样,我将它用作键盘。
我看过这个Dispatch MouseEvent,但我无法解决这个问题。谁能给我一个小例子来证明它是如何工作的?
答案 0 :(得分:2)
以下是全局鼠标侦听器的简单示例:
public static void main ( String[] args )
{
final JFrame frame = new JFrame ();
// Some content
frame.getContentPane ().setLayout ( new FlowLayout ( FlowLayout.CENTER, 5, 5 ) );
frame.getContentPane ().add ( new JButton ( "Test" ) );
frame.getContentPane ().add ( new JLabel ( "Test" ) );
frame.getContentPane ().add ( new JTextField ( "Test" ) );
// Global mouse listener
final AWTEventListener listener = new AWTEventListener ()
{
public void eventDispatched ( AWTEvent event )
{
// Event and component that recieved that event
MouseEvent me = ( MouseEvent ) event;
Component c = me.getComponent ();
// Ignoring mouse events from any other frame
if ( SwingUtilities.getWindowAncestor ( c ) == frame )
{
if ( event.getID () == MouseEvent.MOUSE_PRESSED )
{
System.out.println ( "Mouse pressed on " + c.getClass ().getCanonicalName () );
}
if ( event.getID () == MouseEvent.MOUSE_RELEASED )
{
System.out.println ( "Mouse released on " + c.getClass ().getCanonicalName () );
}
if ( event.getID () == MouseEvent.MOUSE_ENTERED )
{
System.out.println ( "Mouse entered " + c.getClass ().getCanonicalName () );
}
if ( event.getID () == MouseEvent.MOUSE_EXITED )
{
System.out.println ( "Mouse exited " + c.getClass ().getCanonicalName () );
}
}
}
};
Toolkit.getDefaultToolkit ().addAWTEventListener ( listener, AWTEvent.MOUSE_EVENT_MASK );
frame.pack ();
frame.setLocationRelativeTo ( null );
frame.setVisible ( true );
}
请注意,此侦听器将通知您有关任何框架/对话框/窗口/弹出窗口内的每个鼠标事件。这就是为什么你应该将你的动作限制在某个特定的框架,就像我在示例中所做的那样,以防你不需要来自其他框架/窗口的事件/ ...//>
顺便说一下,您可以使用相同的方法轻松地全局收听其他一些事件(例如关键事件)。例如 - 使用AWTEvent.KEY_EVENT_MASK键而不是AWTEvent.MOUSE_EVENT_MASK并将AWTEvent转换为KeyEvent而不是MouseEvent。
答案 1 :(得分:1)
在最好的时候,这将是一个充满混乱和痛苦的世界......
在此处Global Event Listeners with AWTEventListener & how to pull MouseEvent's from it查看替代概念。基本上它是一个全球鼠标监听器。你需要做很多工作才能让它为你工作(检查孩子属于父母等),但它确实有效。
答案 2 :(得分:0)
尝试使用MouseEvent Class
,使用静态方法MOUSE_CLICKED
有关详细信息,请参阅此处:
http://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseEvent.html
请参阅roseindia的这个例子
import java.awt.*;
import java.awt.event.*;
public class MouseClick {
Label lbl;
public static void main(String[] args) {
MouseClick MC = new MouseClick();
}
public MouseClick(){
Frame f = new Frame("Checking the mouse click");
Panel p = new Panel();
Button button = new Button("Click Me");
button.addMouseListener(new MyMouseListener());
p.add(button, BorderLayout.NORTH);
f.add(p,BorderLayout.NORTH);
lbl = new Label("Roseindia.net");
f.add(lbl, BorderLayout.CENTER);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
f.setSize(400,400);
f.setVisible(true);
}
public class MyMouseListener extends MouseAdapter{
public void mouseClicked(MouseEvent me){
String str = lbl.getText();
if (str.equals("Roseindia.net")){
lbl.setText("You have clicke the button.");
}
else if (str.equals("You have clicke the button.")){
lbl.setText("Roseindia.net");
}
}
}
}
请参阅Oracle中的此示例
public class MouseEventDemo ... implements MouseListener {
//where initialization occurs:
//Register for mouse events on blankArea and the panel.
blankArea.addMouseListener(this);
addMouseListener(this);
...
public void mousePressed(MouseEvent e) {
saySomething("Mouse pressed; # of clicks: "
+ e.getClickCount(), e);
}
public void mouseReleased(MouseEvent e) {
saySomething("Mouse released; # of clicks: "
+ e.getClickCount(), e);
}
public void mouseEntered(MouseEvent e) {
saySomething("Mouse entered", e);
}
public void mouseExited(MouseEvent e) {
saySomething("Mouse exited", e);
}
public void mouseClicked(MouseEvent e) {
saySomething("Mouse clicked (# of clicks: "
+ e.getClickCount() + ")", e);
}
void saySomething(String eventDescription, MouseEvent e) {
textArea.append(eventDescription + " detected on "
+ e.getComponent().getClass().getName()
+ "." + newline);
}
}