Achieve effect
Background
For daily brushing, click "Continue Learning" every ten minutes " behavior, or in other words, a repetitive behavior in a stand-alone game that you want to "automate." We can achieve this through the Robot class in JavaFx.
Difficulty
Window penetration
Recording of mouse behavior
Building the program
Required knowledge
Window test
@Override public void start(Stage stage) throws Exception { Scene scene = new Scene(new Pane (), 320, 240); stage.setTitle("按键精灵!"); stage.setScene(scene); stage.show(); }
Get the mouse position
Robot robot = new Robot (); //获得鼠标位置 Point2D mp = robot.getMousePosition (); System.out.println (mp);
Simulate mouse click
Left
MouseButton.PRIMARY
Right
MouseButton.SECONDARY
Scroll wheel
MouseButton.MIDDLE
Simulating mouse movement
//鼠标移动 robot.mouseMove (new Point2D (800,800));
Mouse behavior monitoring
//监听鼠标单击 pane.setOnMouseClicked (e->{ System.out.println (e.getButton ()); System.out.println (e.getSceneX ()); System.out.println (e.getSceneY ()); }); //监听鼠标键入 pane.setOnMousePressed (e->{}); //监听鼠标释放 pane.setOnMouseReleased (e->{}); //监听鼠标在摁着某个键时的拖动 pane.setOnMouseDragged (e->{});
Complete source code and detailed explanation
public class QMApp extends Application { @Override public void start(Stage stage) throws Exception { /** * 内部类,将每次鼠标的行为以及坐标记录下来 */ class MyMouseEvent{ //鼠标行为 MouseButton mb; //类型 EventType et; //坐标 Point2D point2D; public MyMouseEvent(MouseButton mb, EventType et, Point2D point2D) { this.mb = mb; this.et=et; 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 (),e.getEventType (),new Point2D (e.getSceneX (),e.getSceneY ()))); }); pane.setOnMousePressed (e->{ list.add (new MyMouseEvent (e.getButton (),e.getEventType (),new Point2D (e.getSceneX (),e.getSceneY ()))); }); pane.setOnMouseReleased (e->{ list.add (new MyMouseEvent (e.getButton (),e.getEventType (),new Point2D (e.getSceneX (),e.getSceneY ()))); }); }); //点击“结束”按钮时,鼠标行为储存进列表 endb.setOnMouseClicked (e->{ pane.getChildren ().remove (beginb); for (int i = 0; i < list.size (); i++) { //每次鼠标模拟都将窗口透明,实现穿透 stage.setOpacity (0); if (list.get (i).et== MOUSE_DRAGGED){ robot.mousePress (list.get (i).mb); }else { 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); } }
The above is the detailed content of How to implement a button wizard using Java?. For more information, please follow other related articles on the PHP Chinese website!

Java is platform-independent because of its "write once, run everywhere" design philosophy, which relies on Java virtual machines (JVMs) and bytecode. 1) Java code is compiled into bytecode, interpreted by the JVM or compiled on the fly locally. 2) Pay attention to library dependencies, performance differences and environment configuration. 3) Using standard libraries, cross-platform testing and version management is the best practice to ensure platform independence.

Java'splatformindependenceisnotsimple;itinvolvescomplexities.1)JVMcompatibilitymustbeensuredacrossplatforms.2)Nativelibrariesandsystemcallsneedcarefulhandling.3)Dependenciesandlibrariesrequirecross-platformcompatibility.4)Performanceoptimizationacros

Java'splatformindependencebenefitswebapplicationsbyallowingcodetorunonanysystemwithaJVM,simplifyingdeploymentandscaling.Itenables:1)easydeploymentacrossdifferentservers,2)seamlessscalingacrosscloudplatforms,and3)consistentdevelopmenttodeploymentproce

TheJVMistheruntimeenvironmentforexecutingJavabytecode,crucialforJava's"writeonce,runanywhere"capability.Itmanagesmemory,executesthreads,andensuressecurity,makingitessentialforJavadeveloperstounderstandforefficientandrobustapplicationdevelop

Javaremainsatopchoicefordevelopersduetoitsplatformindependence,object-orienteddesign,strongtyping,automaticmemorymanagement,andcomprehensivestandardlibrary.ThesefeaturesmakeJavaversatileandpowerful,suitableforawiderangeofapplications,despitesomechall

Java'splatformindependencemeansdeveloperscanwritecodeonceandrunitonanydevicewithoutrecompiling.ThisisachievedthroughtheJavaVirtualMachine(JVM),whichtranslatesbytecodeintomachine-specificinstructions,allowinguniversalcompatibilityacrossplatforms.Howev

To set up the JVM, you need to follow the following steps: 1) Download and install the JDK, 2) Set environment variables, 3) Verify the installation, 4) Set the IDE, 5) Test the runner program. Setting up a JVM is not just about making it work, it also involves optimizing memory allocation, garbage collection, performance tuning, and error handling to ensure optimal operation.

ToensureJavaplatformindependence,followthesesteps:1)CompileandrunyourapplicationonmultipleplatformsusingdifferentOSandJVMversions.2)UtilizeCI/CDpipelineslikeJenkinsorGitHubActionsforautomatedcross-platformtesting.3)Usecross-platformtestingframeworkss


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

WebStorm Mac version
Useful JavaScript development tools
