如何在JavaFx中将数据从一个场景传输到另一个场景?

时间:2015-10-05 02:45:07

标签: java javafx-8

JavaFX的

我正在使用JavaFX创建我的第一个GUI,我在从第一个场景中的两个文本字段中抓取用户输入并将其传输到下一个场景时遇到问题。我要求输入用户名,并希望在按下提交后将其输入放入下一个场景的标签中。 .getText()似乎没有起作用,因为它说我的变量是NULL。我确信这是非常简单的事我只是在忽视它或者想要努力。 这是我的应用程序的示例代码。

public class TitleScreen extends Application {
Stage window;
Button button;
Button clearButton;
Button bButton, vButton, dButton;
Button backButton, completeButton;
Scene scene, scene1, scene2, scene3;
String first, last;
public static void main(String[] args){
    launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
    window = primaryStage;
    window.setTitle("Flight Assistant");

    /*window.setOnCloseRequest(e -> {
        e.consume();
        closeProgram();
    });*/

    /*
    First scene will ask the User for their name to take that input and place it in the Title
     */
    //Creating a GridPane container
    GridPane grid = new GridPane();
    grid.setPadding(new Insets(10, 10, 10, 10));
    grid.setVgap(5);
    grid.setHgap(5);

    //Defining the Name text field
    final TextField firstName = new TextField();
    firstName.setPromptText("Enter your first name.");
    firstName.setPrefColumnCount(10);
    first = firstName.getText();
    GridPane.setConstraints(firstName, 0, 0);
    grid.getChildren().add(firstName);

    //Defining the Last Name text field
    final TextField lastName = new TextField();
    lastName.setPromptText("Enter your last name.");
    last = lastName.getText();
    GridPane.setConstraints(lastName, 0, 1);
    grid.getChildren().add(lastName);

    //Defining the Submit button
    Button submit = new Button("Submit");
    submit.setOnAction(e-> window.setScene(scene1));
    GridPane.setConstraints(submit, 1, 0);
    grid.getChildren().add(submit);

    //Defining the Clear button
    clearButton = new Button("Clear");
    GridPane.setConstraints(clearButton, 1, 1);
    grid.getChildren().add(clearButton);

    scene = new Scene(grid, 300, 150);

    //Title of scene1
    Label title = new Label();
    title.setText("Welcome " + first + " " + last +" to Flight Assistance!");
    title.setFont(Font.font("Times New Roman", 38));

    //button showing pending request
    button = new Button();
    button.setText("Pending request");
    //switch to scene 2
    button.setOnAction(e -> window.setScene(scene2));

    //Layout 1 and adding elements to the layout
    VBox layout1 = new VBox(200);
    layout1.getChildren().addAll(title, button);
    layout1.setAlignment(Pos.CENTER);

    //setting the layout size for the first scene
    scene1 = new Scene(layout1, 900, 650);

我还尝试将String first = firstName.getText();放在按钮的事件处理程序中,但它给出了一个错误,var从未使用过。

1 个答案:

答案 0 :(得分:1)

您在加载时创建第二个场景&&并且获得价值,你可以做两件事。 1)您可以创建场景onSubmit并获取设置为字段的值 2)在加载时创建场景,但在提交时设置值并在提交中设置标签

第一个解决方案

public class TitleScreen extends Application {
Stage window;
Button button;
Button clearButton;
Button bButton, vButton, dButton;
Button backButton, completeButton;
Scene scene, scene1, scene2, scene3;
String first, last;

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

@Override
public void start(Stage primaryStage) throws Exception {
    window = primaryStage;
    window.setTitle("Flight Assistant");

    /*
     * window.setOnCloseRequest(e -> { e.consume(); closeProgram(); });
     */

    /*
     * First scene will ask the User for their name to take that input and
     * place it in the Title
     */
    // Creating a GridPane container
    GridPane grid = new GridPane();
    grid.setPadding(new Insets(10, 10, 10, 10));
    grid.setVgap(5);
    grid.setHgap(5);

    // Defining the Name text field
    final TextField firstName = new TextField();
    firstName.setPromptText("Enter your first name.");
    firstName.setPrefColumnCount(10);

    GridPane.setConstraints(firstName, 0, 0);
    grid.getChildren().add(firstName);

    // Defining the Last Name text field
    final TextField lastName = new TextField();
    lastName.setPromptText("Enter your last name.");

    GridPane.setConstraints(lastName, 0, 1);
    grid.getChildren().add(lastName);

    // Defining the Submit button
    Button submit = new Button("Submit");
    submit.setOnAction(e -> {
        first = firstName.getText();
        last = lastName.getText();
        createNewScene();
    });
    GridPane.setConstraints(submit, 1, 0);
    grid.getChildren().add(submit);

    // Defining the Clear button
    clearButton = new Button("Clear");
    GridPane.setConstraints(clearButton, 1, 1);
    grid.getChildren().add(clearButton);
    scene = new Scene(grid, 300, 150);
    primaryStage.setScene(scene);
    primaryStage.show();

}

public void createNewScene() {
    Label title = new Label();
    title.setText("Welcome " + first + " " + last + " to Flight Assistance!");
    title.setFont(Font.font("Times New Roman", 38));

    // button showing pending request
    button = new Button();
    button.setText("Pending request");
    // switch to scene 2
    button.setOnAction(e -> window.setScene(scene2));

    // Layout 1 and adding elements to the layout
    VBox layout1 = new VBox(200);
    layout1.getChildren().addAll(title, button);
    layout1.setAlignment(Pos.CENTER);

    // setting the layout size for the first scene
    scene1 = new Scene(layout1, 900, 650);
    window.setScene(scene1);
 }
 }

第二个解决方案

public class ThreeStages extends Application {
Stage window;
Button button;
Button clearButton;
Button bButton, vButton, dButton;
Button backButton, completeButton;
Scene scene, scene1, scene2, scene3;
String first, last;

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

@Override
public void start(Stage primaryStage) throws Exception {
    window = primaryStage;
    window.setTitle("Flight Assistant");

    /*
     * window.setOnCloseRequest(e -> { e.consume(); closeProgram(); });
     */

    /*
     * First scene will ask the User for their name to take that input and
     * place it in the Title
     */
    // Creating a GridPane container
    GridPane grid = new GridPane();
    grid.setPadding(new Insets(10, 10, 10, 10));
    grid.setVgap(5);
    grid.setHgap(5);

    // Defining the Name text field
    final TextField firstName = new TextField();
    firstName.setPromptText("Enter your first name.");
    firstName.setPrefColumnCount(10);

    GridPane.setConstraints(firstName, 0, 0);
    grid.getChildren().add(firstName);

    // Defining the Last Name text field
    final TextField lastName = new TextField();
    lastName.setPromptText("Enter your last name.");

    GridPane.setConstraints(lastName, 0, 1);
    grid.getChildren().add(lastName);

    // Defining the Submit button
    Button submit = new Button("Submit");
    Label title = new Label();
    submit.setOnAction(e -> {
        first = firstName.getText();
        last = lastName.getText();
        title.setText("Welcome " + first + " " + last + " to Flight Assistance!");
        window.setScene(scene1);
    });
    GridPane.setConstraints(submit, 1, 0);
    grid.getChildren().add(submit);

    // Defining the Clear button
    clearButton = new Button("Clear");
    GridPane.setConstraints(clearButton, 1, 1);
    grid.getChildren().add(clearButton);

    scene = new Scene(grid, 300, 150);
    primaryStage.setScene(scene);
    primaryStage.show();
    // Title of scene1

    title.setFont(Font.font("Times New Roman", 38));

    // button showing pending request
    button = new Button();
    button.setText("Pending request");
    // switch to scene 2
    button.setOnAction(e -> window.setScene(scene2));

    // Layout 1 and adding elements to the layout
    VBox layout1 = new VBox(200);
    layout1.getChildren().addAll(title, button);
    layout1.setAlignment(Pos.CENTER);

    // setting the layout size for the first scene
    scene1 = new Scene(layout1, 900, 650);

  }
}

我希望这会对你有所帮助。我测试它正在运行