当前位置:   article > 正文

JavaFx实现键盘、鼠标、按钮的监听_javafx 可以通过实现mouselistener接口中的mousepressed()方法来监听鼠

javafx 可以通过实现mouselistener接口中的mousepressed()方法来监听鼠标按下事件

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

一、简单示例:

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

  1. xml<dependencies>
  2.   <dependency>
  3.       <groupId>org.openjfx</groupId>
  4.       <artifactId>javafx-controls</artifactId>
  5.       <version>16</version>
  6.     </dependency>
  7. </dependencies>

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

  1. import javafx.application.Application;
  2. import javafx.event.ActionEvent;
  3. import javafx.event.EventHandler;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.control.Label;
  7. import javafx.scene.control.TextField;
  8. import javafx.scene.input.KeyEvent;
  9. import javafx.scene.input.MouseEvent;
  10. import javafx.scene.layout.VBox;
  11. import javafx.stage.Stage;
  12. public class JavaFXListenersExample extends Application {
  13. public static void main(String[] args) {
  14. launch(args);
  15. }
  16. @Override
  17. public void start(Stage primaryStage) {
  18. Button button = new Button("点击我");
  19. TextField textField = new TextField();
  20. Label label = new Label("鼠标和键盘事件监听器示例");
  21. // 为按钮添加事件监听器
  22. button.setOnAction(new EventHandler<ActionEvent>() {
  23. @Override
  24. public void handle(ActionEvent event) {
  25. System.out.println("按钮被点击了!");
  26. }
  27. });
  28. // 为文本框添加键盘事件监听器
  29. textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
  30. @Override
  31. public void handle(KeyEvent event) {
  32. System.out.println("键盘按下事件:" + event.getCode());
  33. }
  34. });
  35. // 为标签添加鼠标事件监听器
  36. label.setOnMouseClicked(new EventHandler<MouseEvent>() {
  37. @Override
  38. public void handle(MouseEvent event) {
  39. System.out.println("鼠标点击事件:" + event.getButton());
  40. }
  41. });
  42. VBox root = new VBox(button, textField, label);
  43. Scene scene = new Scene(root, 300, 250);
  44. primaryStage.setTitle("JavaFX 监听器示例");
  45. primaryStage.setScene(scene);
  46. primaryStage.show();
  47. }
  48. }

总结:

在这个示例中,我们创建了一个JavaFX应用程序,其中包含一个按钮、一个文本框和一个标签。

我们为按钮添加了一个`ActionEvent`监听器,当用户单击按钮时,会打印出"按钮被点击了!"。

我们为文本框添加了一个`KeyEvent`监听器,当用户在文本框中按下键盘时,会打印出相应的键盘按键。

我们为标签添加了一个`MouseEvent`监听器,当用户单击标签时,会打印出相应的鼠标按钮。

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

  1. import javafx.application.Application;
  2. import javafx.event.ActionEvent;
  3. import javafx.event.EventHandler;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.control.Label;
  7. import javafx.scene.control.TextField;
  8. import javafx.scene.input.KeyEvent;
  9. import javafx.scene.input.MouseEvent;
  10. import javafx.scene.layout.VBox;
  11. import javafx.stage.Stage;
  12. public class JavaFXListenersExample extends Application {
  13. public static void main(String[] args) {
  14. launch(args);
  15. }
  16. @Override
  17. public void start(Stage primaryStage) {
  18. Button button = new Button("点击我");
  19. TextField textField = new TextField();
  20. Label label = new Label("鼠标和键盘事件监听器示例");
  21. // 为按钮添加事件监听器
  22. button.setOnAction(new EventHandler<ActionEvent>() {
  23. @Override
  24. public void handle(ActionEvent event) {
  25. System.out.println("按钮被点击了!");
  26. }
  27. });
  28. // 为文本框添加键盘事件监听器
  29. textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
  30. @Override
  31. public void handle(KeyEvent event) {
  32. System.out.println("键盘按下事件:" + event.getCode());
  33. }
  34. });
  35. // 为标签添加鼠标事件监听器
  36. label.setOnMouseClicked(new EventHandler<MouseEvent>() {
  37. @Override
  38. public void handle(MouseEvent event) {
  39. System.out.println("鼠标点击事件:" + event.getButton());
  40. }
  41. });
  42. VBox root = new VBox(button, textField, label);
  43. Scene scene = new Scene(root, 300, 250);
  44. primaryStage.setTitle("JavaFX 监听器示例");
  45. primaryStage.setScene(scene);
  46. primaryStage.show();
  47. }
  48. }

JavaFX还支持其他类型的事件监听器,如鼠标移动、拖放等。我们可以根据需要为UI组件添加相应的监听器来实现需求。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号