【问题标题】:Move mouse from current location to the JComponent location将鼠标从当前位置移动到 JComponent 位置
【发布时间】:2019-07-25 08:19:26
【问题描述】:

我想将鼠标指针从当前位置自动移动到指定的 Jcomponent 以进行 GUI 测试(方法中没有数字)

click(JComponent comp){
        robot.mouseMove(200, 50);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
}

【问题讨论】:

    标签: java swing awtrobot gui-testing


    【解决方案1】:

    如果你想测试你的软件,可以使用AssertJ Swing

    如果你只是想确定组件位置需要使用getLocationOnScreen方法

    click(JComponent comp){
        try {
            Point loc = comp.getLocationOnScreen();
            Dimension size = comp.getSize();
            // robot.mouseMove(loc.x, loc.y);
            // move to the center of component
            robot.mouseMove(loc.x + size.width / 2, loc.y + size.height / 2);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
        } catch (Exception e) {
            // if component is not on the screen, an exception be thrown.
            e.printStackTrace();
        }
    }
    

    这是完整的例子:

    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Point;
    import java.awt.Robot;
    import java.awt.event.InputEvent;
    
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.SwingUtilities;
    import javax.swing.WindowConstants;
    
    /**
     * <code>RobotTest</code>.
     */
    public class RobotTest {
    
        private Robot robot;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new RobotTest()::startUp);
        }
    
        private void startUp() {
            try {
                robot = new Robot();
            } catch (Exception e) {
                e.printStackTrace();
            }
            JFrame frm = new JFrame("Autoclick");
            JButton btn1 = new JButton("Click here");
            JButton btn2 = new JButton("Auto click");
            btn1.addActionListener(e -> click(btn2));
            btn2.addActionListener(e -> JOptionPane.showMessageDialog(frm, "Autoclick done!"));
            frm.setLayout(new FlowLayout());
            frm.add(btn1);
            frm.add(btn2);
            frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frm.pack();
            frm.setLocationRelativeTo(null);
            frm.setVisible(true);
        }
    
        private void click(JComponent comp) {
            try {
                Point loc = comp.getLocationOnScreen();
                Dimension size = comp.getSize();
                // robot.mouseMove(loc.x, loc.y);
                // move to the center of component
                robot.mouseMove(loc.x + size.width / 2, loc.y + size.height / 2);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);
            } catch (Exception e) {
                // if component is not on the screen, an exception be thrown.
                e.printStackTrace();
            }
        }
    }
    

    【讨论】:

    • 感谢您的消息,但“robot.mouseMove(loc.x + size.width / 2, loc.y + size.height / 2)”没有将我的鼠标拖到组件上。不知道为什么。
    • @user10478566 我添加了一个示例,以证明 click 方法有效。请构建一个小示例来演示您的用例并将其发布在您的问题中,以便我也可以运行它并了解问题所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 2013-09-22
    • 2017-09-09
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多