赞
踩
在JavaFX中,为UI组件添加鼠标、键盘和按钮事件监听器的方法如下:
- xml<dependencies>
- <dependency>
- <groupId>org.openjfx</groupId>
- <artifactId>javafx-controls</artifactId>
- <version>16</version>
- </dependency>
- </dependencies>
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextField;
- import javafx.scene.input.KeyEvent;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.layout.VBox;
- import javafx.stage.Stage;
-
- public class JavaFXListenersExample extends Application {
-
- public static void main(String[] args) {
- launch(args);
- }
-
- @Override
- public void start(Stage primaryStage) {
- Button button = new Button("点击我");
- TextField textField = new TextField();
- Label label = new Label("鼠标和键盘事件监听器示例");
-
- // 为按钮添加事件监听器
- button.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- System.out.println("按钮被点击了!");
- }
- });
-
- // 为文本框添加键盘事件监听器
- textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
- @Override
- public void handle(KeyEvent event) {
- System.out.println("键盘按下事件:" + event.getCode());
- }
- });
-
- // 为标签添加鼠标事件监听器
- label.setOnMouseClicked(new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent event) {
- System.out.println("鼠标点击事件:" + event.getButton());
- }
- });
-
- VBox root = new VBox(button, textField, label);
- Scene scene = new Scene(root, 300, 250);
-
- primaryStage.setTitle("JavaFX 监听器示例");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- }
总结:
在这个示例中,我们创建了一个JavaFX应用程序,其中包含一个按钮、一个文本框和一个标签。
我们为按钮添加了一个`ActionEvent`监听器,当用户单击按钮时,会打印出"按钮被点击了!"。
我们为文本框添加了一个`KeyEvent`监听器,当用户在文本框中按下键盘时,会打印出相应的键盘按键。
我们为标签添加了一个`MouseEvent`监听器,当用户单击标签时,会打印出相应的鼠标按钮。
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextField;
- import javafx.scene.input.KeyEvent;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.layout.VBox;
- import javafx.stage.Stage;
-
- public class JavaFXListenersExample extends Application {
-
- public static void main(String[] args) {
- launch(args);
- }
-
- @Override
- public void start(Stage primaryStage) {
- Button button = new Button("点击我");
- TextField textField = new TextField();
- Label label = new Label("鼠标和键盘事件监听器示例");
-
- // 为按钮添加事件监听器
- button.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- System.out.println("按钮被点击了!");
- }
- });
-
- // 为文本框添加键盘事件监听器
- textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
- @Override
- public void handle(KeyEvent event) {
- System.out.println("键盘按下事件:" + event.getCode());
- }
- });
-
- // 为标签添加鼠标事件监听器
- label.setOnMouseClicked(new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent event) {
- System.out.println("鼠标点击事件:" + event.getButton());
- }
- });
-
- VBox root = new VBox(button, textField, label);
- Scene scene = new Scene(root, 300, 250);
-
- primaryStage.setTitle("JavaFX 监听器示例");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- }
JavaFX还支持其他类型的事件监听器,如鼠标移动、拖放等。我们可以根据需要为UI组件添加相应的监听器来实现需求。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。