java - EventHandler 在一个单独的类中

标签 java javafx event-handling

以下代码允许在两个场景之间切换。

import javafx.application.*;
import javafx.event.ActionEvent; 
import javafx.event.EventHandler;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;

public class EventHandlersExample extends Application {

  Stage window;
  Scene scene1, scene2;

  public static void main(String[] args) {
      launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
      window = primaryStage;

      //Button 1
      Label label1 = new Label("Welcome to the first scene!");
      Button button1 = new Button("Go to scene 2");

      button1.setOnAction(e -> window.setScene(scene2));

      //Textfield
      TextField tf1 = new TextField();

      //Layout 1 - children laid out in vertical column
      VBox layout1 = new VBox(20);
      layout1.getChildren().addAll(label1, button1);
      scene1 = new Scene(layout1, 200, 200);

      //Button 2
      Button button2 = new Button("Go back to scene 1");
      button2.setOnAction(e -> window.setScene(scene1));

      //Layout 2
      StackPane layout2 = new StackPane();
      layout2.getChildren().add(button2);
      scene2 = new Scene(layout2, 600, 300);

      //Display scene 1 at first
      window.setScene(scene1);
      window.show();
  }
}

但是,我宁愿在单独的类中看到我的 EventHandler,如下所示:

import javafx.application.*;
import javafx.event.ActionEvent; 
import javafx.event.EventHandler;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;

public class EventHandlersExample extends Application {

  Stage window;
  Scene scene1, scene2;

  public static void main(String[] args) {
      launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
      window = primaryStage;

      //Button 1
      Label label1 = new Label("Welcome to the first scene!");
      Button button1 = new Button("Go to scene 2");

      //button1.setOnAction(e -> window.setScene(scene2));
      Button1Handler button1handler = new Button1Handler();
      button1.setOnAction(button1handler);

      //Textfield
      TextField tf1 = new TextField();

      //Layout 1 - children laid out in vertical column
      VBox layout1 = new VBox(20);
      layout1.getChildren().addAll(label1, button1);
      scene1 = new Scene(layout1, 200, 200);

      //Button 2
      Button button2 = new Button("Go back to scene 1");
      button2.setOnAction(e -> window.setScene(scene1));

      //Layout 2
      StackPane layout2 = new StackPane();
      layout2.getChildren().add(button2);
      scene2 = new Scene(layout2, 600, 300);

      //Display scene 1 at first
      window.setScene(scene1);
      window.show();
  }
}

class Button1Handler implements EventHandler<ActionEvent> {
      @Override
      public void handle (ActionEvent e) {
      System.out.println("OK button clicked");
      window.setScene(scene2);  
    }
  }

但是,在 EventHandlersExample 类中对窗口的引用不起作用。

当我尝试从 EventHandlersExample-Class 中的文本字段执行 textfield.getText() 时,(当然)会发生同样的问题。

我该如何解决这个问题?---非常感谢您的帮助。

最佳答案

如果您将事件处理逻辑封装在单独的类中,则需要以某种方式将父窗口和场景的引用传递给处理程序。执行此操作的一种便捷方法是通过构造函数注入(inject)依赖项:

class Button1Handler implements EventHandler<ActionEvent> {

    private final Stage window;
    private final Scene scene;

    public Button1Handler (Stage window, Scene scene) {
        this.window = window;
        this.scene = scene;
    }

     @Override
     public void handle (ActionEvent e) {
         System.out.println("OK button clicked");
         window.setScene(scene);  
    }
}

然后,您可以使用 new Button1Handler(window, scene2) 创建处理程序。

关于java - EventHandler 在一个单独的类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51534680/

相关文章:

java - N 数串接 N 次的模数

java - 在 JavaFX 中使用外部线程在 TextFlow 上添加元素

javafx - 在哪里可以找到皮肤类的 JavaFX 8 源代码?

javafx - DrawCircle (GraphicsContext gc) 与 Javafx 中的 Canvas

javascript - 如何用jquery获取下一个li数据值?

java - 无法下载图片

java - Eclipse 插件开发的正确日志记录

c++ - 当鼠标单击可编辑文本框时如何在 C++ 中显示通知

c# winforms events 在转义时恢复文本框内容

java - 阻止 JTextPane 接收新换行符