0
我可以模擬鼠標拖動的事件嗎?我有一個擴展自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);
}
}
試試吧。您將生成mousePressed事件,然後生成mouseMoved事件,然後生成mouseReleased事件。看看會發生什麼,並給我們你的結果。 – camickr
我不知道如何新建一個fittable MouseEvent .... – lution
搜索論壇/網絡例如使用Robot類。 – camickr