JavaFX Button和Scene点击事件代码示例
2021/7/2 11:22:16
本文主要是介绍JavaFX Button和Scene点击事件代码示例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
最终效果 代码写在一个类里面
package cn.zxl.StackPane; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.input.MouseEvent; import javafx.scene.layout.StackPane; import javafx.stage.Stage; /** * @Description: //TODO * @Author: zhangxueliang * @Create: 2021-05-27 09:04 * @Version: 1.0 **/ public class MyStackPane extends Application implements EventHandler<MouseEvent> { Button btn; @Override public void start(Stage primaryStage) throws Exception { btn = new Button("你好"); btn.setOnMouseClicked(this); StackPane stackPane = new StackPane(); stackPane.getChildren().add(btn); Scene scene = new Scene(stackPane, 200, 200); scene.setOnMousePressed(this); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } @Override public void handle(MouseEvent event) { if (event.getSource()==btn){ System.out.println("你点击的是按钮"); }else { System.out.println("你点击了场景"); } } }也可以单独提取出事件监听类
按钮事件监听类
package cn.zxl.StackPane; import javafx.event.EventHandler; import javafx.scene.control.Button; import javafx.scene.input.MouseEvent; /** * @Description: //TODO * @Author: zhangxueliang * @Create: 2021-05-27 09:20 * @Version: 1.0 **/ public class MyMouseEvent implements EventHandler<MouseEvent> { @Override public void handle(MouseEvent event) { System.out.println("你点击的是按钮"); } }
场景事件监听类
package cn.zxl.StackPane; import javafx.event.EventHandler; import javafx.scene.input.MouseEvent; /** * @Description: //TODO * @Author: zhangxueliang * @Create: 2021-05-27 09:24 * @Version: 1.0 **/ public class SceneMouseEvent implements EventHandler<MouseEvent> { @Override public void handle(MouseEvent event) { System.out.println("你点击的是场景"); } }
调用
package cn.zxl.StackPane; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; /** * @Description: //TODO * @Author: zhangxueliang * @Create: 2021-05-27 09:04 * @Version: 1.0 **/ public class MyStackPane2 extends Application{ Button btn; @Override public void start(Stage primaryStage) throws Exception { btn = new Button("你好"); btn.setOnMouseClicked(new MyMouseEvent()); StackPane stackPane = new StackPane(); stackPane.getChildren().add(btn); Scene scene = new Scene(stackPane, 200, 200); scene.setOnMousePressed(new SceneMouseEvent()); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }匿名内部类方式(推荐)
package cn.zxl.StackPane; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.input.MouseEvent; import javafx.scene.layout.StackPane; import javafx.stage.Stage; /** * @Description: //TODO * @Author: zhangxueliang * @Create: 2021-05-27 09:04 * @Version: 1.0 **/ public class MyStackPane3 extends Application{ Button btn; @Override public void start(Stage primaryStage) throws Exception { btn = new Button("你好"); btn.setOnMouseClicked(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { System.out.println("你点击的是按钮"); } }); StackPane stackPane = new StackPane(); stackPane.getChildren().add(btn); Scene scene = new Scene(stackPane, 200, 200); scene.setOnMousePressed(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { System.out.println("你点击的是场景"); } }); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }lambda表达式(新手不推荐)
package cn.zxl.StackPane; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.input.MouseEvent; import javafx.scene.layout.StackPane; import javafx.stage.Stage; /** * @Description: //TODO * @Author: zhangxueliang * @Create: 2021-05-27 09:04 * @Version: 1.0 **/ public class MyStackPane4 extends Application{ Button btn; @Override public void start(Stage primaryStage) throws Exception { btn = new Button("你好"); btn.setOnMouseClicked(event -> { System.out.println("你点击的是按钮"); }); StackPane stackPane = new StackPane(); stackPane.getChildren().add(btn); Scene scene = new Scene(stackPane, 200, 200); scene.setOnMousePressed(event -> { System.out.println("你点击的是场景"); }); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
这篇关于JavaFX Button和Scene点击事件代码示例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-08如何用关键链方法突破项目管理瓶颈?
- 2025-01-08电商人必看!6 款提升团队协作与客户满意度软件!
- 2025-01-08电商团队管理混乱?快用这 6 款软件优化协作流程!
- 2025-01-08短剧制作效率低?试试这5款任务管理工具
- 2025-01-08高效应对电商高峰,6 款团队协作软件大揭秘!
- 2025-01-08为什么外贸人都爱上了在线协作工具?
- 2025-01-08提升工作效率,从这些任务管理工具开始
- 2025-01-08新年电商订单暴增,必备的 6 款可视化协作办公软件有哪些?
- 2025-01-08短剧制作经理必备技能与工具全解析
- 2025-01-08在线协作让年货大促轻松应对!