gpt4 book ai didi

java - SwingUtilities.getLocationOnScreen() 的问题;

转载 作者:行者123 更新时间:2023-11-30 05:57:15 24 4
gpt4 key购买 nike

我在 JButton“支持”上的 ActionListener 中有以下代码。 cl.across.text 是我的 GUI 中的另一个 JTextArea。问题在于点 pt1 给出的结果。 Frame 为 600x600,按钮位于中间按钮,JTextArea 位于右中位置。打印命令的结果是:java.awt.Point[x=501,y=187] java.awt.Point[x=370,y=1062]现在第一个坐标是正确的(用 MouseListener 检查),但第二个坐标完全超出范围,它们甚至超出了我的框架(考虑它 600 和 y=1062)。任何关于如何获得正确的建议,因为我需要制作一个按下 is 的机器人,这是我通过调整 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);

最佳答案

我认为您获得不正确的点坐标的原因是方法 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);

示例:在此示例中,您可以看到 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);
}
}
}

关于java - SwingUtilities.getLocationOnScreen() 的问题;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6053663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com