JavaFx实现键盘、鼠标、按钮的监听

在JavaFX中,为UI组件添加鼠标、键盘和按钮事件监听器的方法如下:

一、简单示例:

1. 首先,确保已经导入了JavaFX库。在`pom.xml`文件中添加以下依赖(如果使用Maven):

java 复制代码
xml<dependencies>
  <dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-controls</artifactId>
      <version>16</version>
    </dependency>
</dependencies>

2.创建一个JavaFX应用程序,并在其中添加一个按钮、一个文本框和一个标签。为按钮、文本框和标签添加鼠标、键盘和按钮事件监听器。

java 复制代码
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`监听器,当用户单击标签时,会打印出相应的鼠标按钮。

二、复杂监听(鼠标--单击 、按钮--点击、键盘--打印按下内容)

java 复制代码
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组件添加相应的监听器来实现需求。

相关推荐
迷糊小鬼3 分钟前
Button matrix(矩阵按钮) (lv_buttonmatrix)
c语言·开发语言·前端·ui·矩阵
MicoZone4 分钟前
源码-redisson
java
南境十里·墨染春水6 分钟前
C++ 笔记:std::bind 函数模板详解
前端·c++·笔记
happymaker06267 分钟前
请求头 & 文件下载 & JSP 内置对象实战
java·前端·servlet
skywalk81638 分钟前
Kotti Next的tinyfrontend前端生成和测试(使用WorkBuddy)
前端
m0_647057968 分钟前
【无标题】
前端·人工智能
北城笑笑8 分钟前
Frontend 与 FPGA 深度融合实战解析:从技术协同到多场景落地( 前端和现场可编程门阵列 )
前端·websocket·3d·vue·fpga
lly2024069 分钟前
TypeScript 运算符
开发语言
故事和你919 分钟前
洛谷-算法1-1-模拟与高精度2
开发语言·数据结构·c++·算法·动态规划