我需要在 JLabel(或任何其他组件)上触发 MouseEvent mouseClicked。我该怎么做呢?
我使用 Robot 类进行了如下尝试:
try {
Robot r=new Robot();
r.mouseMove(jl.gettX(), jl.getY());//jl is the JLabel
r.mousePress(InputEvent.BUTTON1_MASK);
r.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但是getX()
andgetY()
正在返回x,y
组件父级的位置。如何获取组件的绝对位置以便触发Robot.mousePress()
?有没有其他方法可以触发特定组件上的事件?