SwingUtilities.getLocationOnScreen()的问题;

时间:2011-05-19 04:10:48

标签: 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);

2 个答案:

答案 0 :(得分:5)

我认为你得到错误的点坐标的原因是方法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);
        }
    }
}

答案 1 :(得分:1)

'pt'是一个屏幕坐标点。因此,您希望使用SwingUtilities.convertPointFromScreen(pt,frame)将其转换为Frame的坐标。