Ale*_*tev 2 java swing actionlistener
我在JButton"支持"上的ActionListener中有以下代码.cl.across.text是我GUI中的另一个JTextArea.问题在于针对点pt1给出的结果.帧为600x600,按钮位于中间,而JTextArea位于中间右侧.print命令的结果是:java.awt.Point [x = 501,y = 187] java.awt.Point [x = 370,y = 1062]现在第一个坐标是正确的(使用MouseListener检查)但第二个坐标是正确的一个完全超出范围,它们甚至在我的框架之外(考虑它600和y = 1062).任何建议如何获得正确的,因为我需要制作一个按下的机器人,这是我唯一的想法,通过调整GUI的大小.
码:
Point pt=new Point(cl.across.text.getLocation());
SwingUtilities.convertPointToScreen(pt, cl.across.text);
Point pt1=new Point(support.getLocation());
SwingUtilities.convertPointToScreen(pt1, support);
System.out.println(pt+" "+pt1);
Run Code Online (Sandbox Code Playgroud)
我认为你得到错误的点坐标的原因是该方法的错误使用SwingUtilities.convertPointToScreen(point, component);.
不要担心我第一次使用这种方法时犯了同样的错误.:)
从方法Component.getLocation()的描述中我们得到它返回:"Point的一个实例,表示组件父级坐标空间中组件边界的左上角"
因此,作为组件参数,我们需要给它的父对象,例如 SwingUtilities.convertPointToScreen(point, component.getParent());
因此,对于您的情况,您将拥有:
Point pt=new Point(cl.across.text.getLocation());
SwingUtilities.convertPointToScreen(pt, cl.across.text.getParent());
Point pt1=new Point(support.getLocation());
SwingUtilities.convertPointToScreen(pt1, support.getParent());
System.out.println(pt+" "+pt1);
Run Code Online (Sandbox Code Playgroud)
示例:在此示例中,您可以看到getLocationOnScreen()如何"足够好"以使机器人完成其工作,并返回与"正确"使用的SwingUtilities.convertPointToScreen()方法返回的结果相同的结果 .
要看到它工作,请启动示例并将手从鼠标上移开并等待几秒钟.
import java.awt.AWTException;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class RobotLocOnScreenTest{
public static void main(String[] args){
final JTextArea ta = new JTextArea(21, 12);
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
JFrame f = new JFrame();
JPanel p = new JPanel();
JTextField tf = new JTextField("asadasdasd", 15);
p.add(tf);
p.add(ta);
p.add(new JTextField(11));
f.setContentPane(p);
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
//request focus so the text field tf has it first
tf.requestFocusInWindow();
}
});
/* A hack to allow the GUI to build so we can see all robot's operations on the area
* and avoid the IllegalComponentStateException exception thrown by
* Component.getLocationOnScreen() method when the component is not showing.
*/
try{
Thread.sleep(2000);
}catch(InterruptedException ex) {
Logger.getLogger(RobotLocOnScreenTest.class.getName()).log(Level.SEVERE, null, ex);
}
findAndOperateOnTextArea(ta);
}
private static void findAndOperateOnTextArea(JTextArea ta){
try{
Robot robot = new Robot();
Point taLOSP = ta.getLocationOnScreen();
Point taLPBad = ta.getLocation();
SwingUtilities.convertPointToScreen(taLPBad, ta);
Point taLPGood = ta.getLocation();
SwingUtilities.convertPointToScreen(taLPGood, ta.getParent());
System.out.println("ta.getLocationOnScreen()=" + taLOSP
+ "; taLPBad=" + taLPBad+"; taLPGood="+taLPGood);
robot.mouseMove(taLOSP.x, taLOSP.y);
robot.delay(1111);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(333);
robot.keyPress(KeyEvent.VK_0);
robot.delay(333);
robot.keyPress(KeyEvent.VK_1);
robot.delay(333);
robot.keyPress(KeyEvent.VK_2);
robot.delay(333);
robot.keyPress(KeyEvent.VK_3);
robot.delay(333);
robot.keyPress(KeyEvent.VK_4);
robot.delay(333);
robot.keyPress(KeyEvent.VK_5);
robot.delay(333);
robot.keyPress(KeyEvent.VK_6);
robot.delay(333);
robot.keyPress(KeyEvent.VK_7);
robot.delay(333);
robot.keyPress(KeyEvent.VK_8);
robot.delay(333);
robot.keyPress(KeyEvent.VK_9);
}catch(AWTException ex){
Logger.getLogger(RobotLocOnScreenTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4162 次 |
| 最近记录: |