简体   繁体   English

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

[英]How do I transfer data from one scene to another in JavaFx?

JavaFX JavaFX

I am creating my first GUI using JavaFX and I am having a problem grabbing user input from two text fields from my first scene and transferring it to the next scene. 我正在使用JavaFX创建我的第一个GUI,但是在从第一个场景的两个文本字段中获取用户输入并将其传输到下一个场景时遇到了问题。 I am asking for the Users name and wanting to place their input into the label of the very next scene after they press submit. 我要询问用户名,并希望在他们按提交后将输入的内容放到下一个场景的标签中。 .getText() does not seem to be working since it says that my variables are NULL . .getText()似乎不起作用,因为它说我的变量为NULL I am sure it is something pretty simple I am just overlooking it or thinking to hard about it. 我敢肯定,这是一件非常简单的事情,我只是忽略了它或在认真考虑。 Here is the sample code of my application. 这是我的应用程序的示例代码。

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);

I have also tried placing the String first = firstName.getText(); 我也尝试过将String first = firstName.getText();放置在String first = firstName.getText(); inside the event handler for the button but it gives me an error that the var has never been used. 在按钮的事件处理程序内部,但它给我一个错误,表明该var从未使用过。

Your creating second scene on load && and getting values too, two things you can do. 在load &&上创建第二个场景并获取值,您可以做两件事。 1) you can create scene onSubmit and get values set to fields 2) create scene on load and but set values on submit and set it label in submit 1)您可以创建场景onSubmit并将值设置为字段2)在加载时创建场景,但在提交时设置值,并在提交时设置标签

First solution 第一个解决方案

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);
 }
 }

Second solution 第二解决方案

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);

  }
}

i hope this will help u. 我希望这对你有帮助。 i tested it is working 我测试过了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中将数据从一项活动转移到另一项活动? - How do I transfer data from one activity to another in Android? 如何将解析的数组数据从一个 Class 传输到另一个? - How Do I Transfer My Parsed Array Data From One Class To Another? 如何将容器从一个 window 转移到另一个 JavaFX - How to transfer a container from one window to another in JavaFX Javafx:如何从一个 class 调用另一个方法? - Javafx: How do I call an method from one class to another? JavaFX - 如何使用另一个类的场景更改场景? - JavaFX - How to change scene using a scene from another class? 如何删除javafx中舞台中的场景? - How do I remove a scene in a stage in javafx? JavaFX:如何在新场景中从observableArrayList更新选定的项。 在不传递整个数据集的情况下呢? - JavaFX: How do I update a selected Item from observableArrayList, in a new Scene. Without passing the in the entire set of data backing it? 如何使用 Java SFTP 库 JSch 将文件从一个目录传输到另一个目录? - How do I transfer a file from one directory to another using Java SFTP Library JSch? 如何将一个JLabel的内容传输到另一个? - How do I transfer the contents of one JLabel to another? 如何将套接字连接从一个套接字通道转移到另一个? - How do you transfer a socket connection from one socketchannel to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM