本文介绍了模拟MouseEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想模拟鼠标在图形上的点击.我添加了Mouselistener,并在完成mouseclick时采取了一些措施,但我确实需要模拟用户在程序中单击了我的图形. /p>

实际上,当您单击一个名为"Clean"的Jbutton时,我想清洁"Graphics 2D canvas".但事实是,只有在用户单击我的"Graphics 2D canvas"时,才可以执行清洁操作.我想产生一种幻觉,即单击JButton可以清除"Graphics 2D canvas".

谢谢.

     addMouseListener(this);
     addMouseMotionListener(this);


public void mousePressed(MouseEvent e) {
            e.consume();
            x1=e.getX();
            y1=e.getY();
            if(figure==1 || figure==3 )   {x2=x1; y2=y1;}
            ;   }

PS:我不能使用机器人,因为我必须在每个操作系统上运行我的程序,而有人告诉我我不能在每个程序上运行它:

Robot robot = null;
try {
    robot = new Robot();
} catch (AWTException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

 // SET THE MOUSE X Y POSITION
 robot.mouseMove(65*Fond_noir.pourcent_largeur, 16*Fond_noir.pourcent_hauteur);
 robot.mousePress(InputEvent.BUTTON1_MASK);
 robot.mouseRelease(InputEvent.BUTTON1_MASK);

}
解决方案

好吧,您对Robot的理解是正确的.它依赖于平台,并且不能保证它将支持JavaDoc上所有平台上的所有功能:

要模拟点击,您只需执行以下操作:

JButton buttonToSimulateClicking = new JButton(...);
buttonToSimulateClicking.doClick(); // As simple as that !

如果您必须模拟点击困难的方式",即模拟鼠标点击,则始终可以执行以下操作:

MouseEvent clickEvent = new MouseEvent(buttonToSimulateClicking, MouseEvent.MOUSE_CLICKED, ...);

EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
eventQueue.dispatchEvent(clickEvent);

I'd like to simulate a Mouse click on a Graphic. I added a Mouselistener, and some action when the mouseclick is done, but I really need to simulate that the user clicked on my Graphic in my programm... How can I say something like "" MouseEvent e is performed!"" ?

Actually I'd like to clean a "Graphics 2D canvas" when you click on a Jbutton called "Clean". But the thing is that the cleaning action would be done only if the user click on my "Graphics 2D canvas". I'd like to make the illusion that the "Graphics 2D canvas" was cleaned by clicking on the JButton..

Thanks.

     addMouseListener(this);
     addMouseMotionListener(this);


public void mousePressed(MouseEvent e) {
            e.consume();
            x1=e.getX();
            y1=e.getY();
            if(figure==1 || figure==3 )   {x2=x1; y2=y1;}
            ;   }

PS : I can't use robot because I have to run my programm on every OS, and someone told me I can't run this on every programm :

Robot robot = null;
try {
    robot = new Robot();
} catch (AWTException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

 // SET THE MOUSE X Y POSITION
 robot.mouseMove(65*Fond_noir.pourcent_largeur, 16*Fond_noir.pourcent_hauteur);
 robot.mousePress(InputEvent.BUTTON1_MASK);
 robot.mouseRelease(InputEvent.BUTTON1_MASK);

}
解决方案

Well, you're right about Robot. It's platform dependent, and there are no guarantees that it will support all features on all platforms, from the JavaDoc:

To simulate the click, you can simply do this:

JButton buttonToSimulateClicking = new JButton(...);
buttonToSimulateClicking.doClick(); // As simple as that !

If you have to simulate the click "the hard way", i.e. to simulate a mouse click, you can always do the following:

MouseEvent clickEvent = new MouseEvent(buttonToSimulateClicking, MouseEvent.MOUSE_CLICKED, ...);

EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
eventQueue.dispatchEvent(clickEvent);

这篇关于模拟MouseEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 14:07