public class QMApp extends Application {
@Override
public void start(Stage stage) throws Exception {
/**
* 内部类,将每次鼠标的行为以及坐标记录下来
*/
class MyMouseEvent{
//鼠标行为
MouseButton mb;
//坐标
Point2D point2D;
public MyMouseEvent(MouseButton mb, Point2D point2D) {
this.mb = mb;
this.point2D = point2D;
}
}
//创建面板
VBox pane = new VBox ();
//对齐方式
pane.setAlignment (Pos.TOP_LEFT);
//robot关键对象
Robot robot = new Robot ();
//记录鼠标行为
LinkedList<MyMouseEvent> list = new LinkedList<> ();
//开始
Button beginb = new Button ("开始");
beginb.setTextFill (Color.RED);
//结束
Button endb = new Button ("结束");
endb.setTextFill (Color.RED);
pane.getChildren ().addAll (beginb,endb);
//点击“开始”按钮时,鼠标行为储存进列表
beginb.setOnMouseClicked (m->{
pane.setOnMouseDragged (e->{
list.add (new MyMouseEvent (e.getButton (),new Point2D (e.getSceneX (),e.getSceneY ())));
});
pane.setOnMousePressed (e->{
list.add (new MyMouseEvent (e.getButton (),new Point2D (e.getSceneX (),e.getSceneY ())));
});
pane.setOnMouseReleased (e->{
list.add (new MyMouseEvent (e.getButton (),new Point2D (e.getSceneX (),e.getSceneY ())));
});
});
//点击“结束”按钮时,鼠标行为储存进列表
endb.setOnMouseClicked (e->{
pane.getChildren ().remove (beginb);
for (int i = 0; i < list.size (); i++) {
//每次鼠标模拟都将窗口透明,实现穿透
stage.setOpacity (0);
robot.mouseMove (list.get (i).point2D);
robot.mouseClick (list.get (i).mb);
try {
//为了效果明显,进行延迟
Thread.sleep (50);
} catch (InterruptedException ex) {
ex.printStackTrace ();
}
}
});
//面板不参与计算边界。鼠标点击事件发生后,会计算应该是哪个组件位于鼠标所在点的位置,而该面板因为不参与边界计算,所以也不会捕获到鼠标事件
pane.setPickOnBounds(false);
//窗口在前,不能拖到,都没有
stage.initStyle (StageStyle.UNDECORATED);
//窗口最大化
stage.setMaximized (true);
//窗口透明度,为了使得开始按钮不会消失同时可以看见其他窗口,半透明
stage.setOpacity (0.3);
Scene scene = new Scene(pane);
stage.setTitle("按键精灵");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch (args);
}
}
setTextFill那里,还有setOnMouseClicked那里还有addAll后边括号里两个参数,还有下面三个获取坐标的new Point2D,还有下面实现穿透那里list.get(i).point2D,都报错了,我头都大了。