首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwingUtilities.getLocationOnScreen()的问题;

SwingUtilities.getLocationOnScreen()的问题;
EN

Stack Overflow用户
提问于 2011-05-19 12:10:48
回答 2查看 4.5K关注 0票数 2

我在JButton "support“上的ActionListener中有以下代码。cl.across.text是我的图形用户界面中的另一个JTextArea。问题出在点pt1的结果上。框架是600x600,按钮在中间按钮,而JTextArea在中间右边。打印命令的结果是: java.awt.Pointx=501,y=187 java.awt.Pointx=370,y=1062现在第一个坐标是正确的(用MouseListener检查),但第二个坐标完全超出了范围,它们甚至超出了我的框架(考虑它是600和y=1062)。任何建议如何获得正确的,因为我需要做一个机器人,按下是,这是我唯一的想法,以获得它与调整图形用户界面。

代码:

代码语言:javascript
复制
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);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-19 17:14:16

我认为你得到不正确的点坐标的原因是SwingUtilities.convertPointToScreen(point, component);方法的不正确使用。

不要担心,当我第一次使用这个方法时,我犯了同样的错误。:)

从对方法Component.getLocation()的描述中,我们可以得到它返回的内容:“一个Point的实例,表示组件的父级坐标空间中组件边界的左上角。”

因此,作为组件参数,我们需要给它的父对象,例如SwingUtilities.convertPointToScreen(point, component.getParent());

因此,对于您的情况,您将拥有:

代码语言:javascript
复制
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()方法返回的结果相同。

要查看它的工作情况,请从示例开始,将手从鼠标上移开,等待几秒钟。

代码语言:javascript
复制
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);
        }
    }
}
票数 5
EN

Stack Overflow用户

发布于 2011-05-19 13:49:14

'pt‘是一个屏幕坐标点。所以你想用SwingUtilities.convertPointFromScreen(pt,frame)把它转换成你的帧的坐标。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6053663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档