如何通过程序而不是鼠标自动模拟(定义)鼠标拖动事件

时间:2017-09-26 15:53:15

标签: java swing mouseevent mouse robot

我可以模拟鼠标拖动的事件吗?我有一个从JPanel扩展的自定义视图,我添加了一个mouseMotionListener,它为它实现了mouseDragged方法。现在,我想为这个组件定义(新)鼠标拖动事件,以便它可以像拖动一样,而不是真正使用鼠标。 我在Oracle.com上搜索了MouseEvent类,但我自己无法定义鼠标拖动事件,你能给我一些想法吗? 我已经实现了一个示例程序,这是我的代码, 这是我的自定义视图,它实现了鼠标拖动方法:

public class Rect extends JLabel {
private int width, height;
private String title;
public int interWidth = 1500;
public int interHeight = 1000;
private JFrame frame;
private MouseMotionAdapter myAdapter;
private MouseListener myListener;

public Rect(int width, int height, String title, JFrame frame) {
    this.width = width;
    this.height = height;
    this.title = title;
    setText(this.title);
    this.frame = frame;
    this.myAdapter = new MouseMotionAdapter(){
        @Override
        public void mouseDragged(MouseEvent me) {
            System.out.println("dragged");
            me.translatePoint(me.getComponent().getLocation().x, me.getComponent().getLocation().y);
            setLocation(me.getX(), me.getY());
            frame.repaint();
        }
    };
    this.myListener = new MouseListener(){

        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub
            System.out.println("click");
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub
            System.out.println("press");
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub
            System.out.println("release");

        }

    };
    addMouseMotionListener(this.myAdapter);
    addMouseListener(this.myListener);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.setColor(Color.black);
    g.drawRoundRect(0, 0, this.width - 1, this.height - 1, 20, 20);
}

public void setWidth(int width) {
    this.width = width;
}

public void setHeight(int height) {
    this.height = height;
}

public int getWidth() {
    return width;
}

public int getHeight() {
    return height;
}

public MouseMotionAdapter getMyAdapter() {
    return myAdapter;
}

public MouseListener getMyListener() {
    return myListener;
}

}

这是主框架包含Rect,您可以通过鼠标拖动rect,但我想通过程序模拟拖动的事件:

public class MainFrame extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainFrame frame = new MainFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public MainFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 648, 518);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    Rect rect = new Rect(120,120,"example",this);
    contentPane.add(rect);

    Robot robot = new Robot();
    Point point = new Point(200,200);
    robot.mouseMove(point.x,point.y);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseMove(300, 300);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
}

}

1 个答案:

答案 0 :(得分:0)

Robot robot;
try {
    robot = new Robot();
    Point point = rect.getLocationOnScreen(); //rect is my custom view
    robot.mouseMove(point.x,point.y);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseMove(point.x + 1,point.y + 1);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);//press+move+release = drag
} catch (AWTException e1) {
    e1.printStackTrace();
}