如何通过程序而不是鼠标自动模拟(定义)鼠标拖动的事件
问题描述:
我可以模拟鼠标拖动的事件吗?我有一个扩展自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;
}
}
这是主框架包含了矩形,可以拖动矩形通过鼠标,但我想通过模拟程序拖事件:
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);
}
}
答
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();
}
试试吧。您将生成mousePressed事件,然后生成mouseMoved事件,然后生成mouseReleased事件。看看会发生什么,并给我们你的结果。 – camickr
我不知道如何新建一个fittable MouseEvent .... – lution
搜索论坛/网络例如使用Robot类。 – camickr