Java 将鼠标从当前位置移动到JComponent位置

Java 将鼠标从当前位置移动到JComponent位置,java,swing,awtrobot,gui-testing,Java,Swing,Awtrobot,Gui Testing,我想将鼠标指针从当前位置自动移动到指定的Jcomponent进行GUI测试(方法中没有数字) 如果你想测试你的软件,你可以使用 如果只想确定组件位置,则需要使用方法getLocationOnScreen click(JComponent comp){ try { Point loc = comp.getLocationOnScreen(); Dimension size = comp.getSize(); // robot.mouseMov

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


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

如果只想确定组件位置,则需要使用方法
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方法的工作原理。请构建一个小示例来演示您的用例,并将其发布到您的问题中,这样我也可以运行它并了解问题所在。
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();
        }
    }
}