【发布时间】:2018-03-07 22:35:00
【问题描述】:
我可以模拟鼠标拖动事件吗?我有一个从 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);
}
}
【问题讨论】:
-
试试看。您将生成一个 mousePressed 事件,然后是一个 mouseMoved 事件,然后是一个 mouseReleased 事件。看看会发生什么,然后给我们你的结果。
-
我不知道如何新建一个适合的 MouseEvent....
-
搜索论坛/网站,例如使用 Robot 类。
-
您最初的问题标题表明您想模拟使用
Robot类拖动的鼠标。你读过 Robot API 吗?您没有定义“鼠标拖动”事件。您调用机器人上的方法。有mousePressed、mouseMoved、mouseReleased方法。抱歉,如果我的原始评论中不清楚的话。我假设您实际上已经阅读过 API 并且熟悉 Robot 类的方法。如果您搜索过论坛/网络,您会发现使用这些方法的示例。所以你可以自己测试它是否有效。 -
如果你不能让它工作,那么发布一个minimal reproducible example,显示你已经尝试过的东西。
标签: java swing mouseevent mouse robot