java - 如何通过关闭一个阶段并返回到上一个阶段的值而不打开一个新阶段?

标签 java javafx scenebuilder

我希望当我单击插入按钮(在第一阶段)时,它会打开一个新阶段,我想在其中输入一个数字并单击插入,它会关闭第二个阶段并返回到第一个窗口并在其中打印插入的数字链接列表。
一切工作正常,除了当我在第二阶段单击插入按钮时,它不会返回到第一个阶段,而是打开新阶段并显示插入的数字,这样,如果我添加多个数字,它只会输出我添加的最新号码。所以我想知道当我单击插入(在第二阶段)时如何返回到上一阶段而不打开新阶段。

这是我的主要代码:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class AppletProject extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("LLApplet.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

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

}

这是我的第一阶段 Controller 的代码(主页):

public class HomeController implements Initializable {

    @FXML
    private Button insertbutton;
    @FXML
    private TextArea outputTextArea;

    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    } 

    @FXML
    private void insertButton(ActionEvent event) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("Insert Screen.fxml"));

        Stage stage = new Stage();
        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();

    }

    public void insert(int d) {
        Node newNode = new Node(d);
        newNode.setNext(head);
        if (head != null){
            head.setPrevious(newNode);
        }
        head = newNode ;
        outputTextArea.setText(displayList().toString());
    }

    public StringBuilder displayList(){
        StringBuilder str = new StringBuilder();
        Node iterator = head ;
        while (iterator != null){
            Print print = new Print(iterator.getData());
                str.append(print);
            iterator = iterator.getNext() ;
            if (iterator != null)
                str.append("->");
        }
        str.append("\n");
        return str;
    }
}

这是我的第二阶段 Controller 的代码(插入屏幕):

public class InsertScreenController implements Initializable {

    @FXML
    private TextField insertTextField;
    @FXML
    private Button insertButton;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    } 
    HomeController home = new HomeController();
    Node head = home.head;
    @FXML
    private void insertButton(ActionEvent event) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("Home.fxml"));

            Parent root = (Parent) loader.load();
            HomeController home = loader.getController();
            home.output(Integer.parseInt(insertTextField.getText()));


            Scene scene = new Scene(root);
            Stage window = (Stage)((javafx.scene.Node)event.getSource()).getScene().getWindow();

            window.setScene(scene);
            window.close();
                    } catch (IOException ex) {
            Logger.getLogger(InsertScreenController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    }
    Here is my Node Class:


    public class Node {
    private int data ;
    private Node next ;
    private Node previous ;

    public Node(){
        data = 0 ;
        next = null ;
    }

    public Node (int data){
        this.data = data ;
    }

    public Node (int data, Node next, Node previous){
        this.data = data ;
        this.next = next ;
        this.previous = previous ;
    }


    public int getData() {
        return data;
    }

    public Node getNext() {
        return next;
    }

    public Node getPrevious(){
        return previous ;
    }

    public void setData(int data) {
        this.data = data;
    }

    public void setNext(Node next) {
        this.next = next;
    }   

    public void setPrevious(Node previous){
        this.previous = previous ; 
    } 
}

这是我的打印类:

class Print{
    int data;

    public Print(int data) {
        this.data = data;
    }

    @Override
    public String toString(){
        return String.format("[%d]",data);
    }
}

最佳答案

您将重新加载场景并通过单击插入按钮创建一个新窗口。您需要做的是与之前创建的场景进行通信。您可以通过将合适的对象(或多个对象)传递到新场景来实现此目的,如问题 Passing Parameters JavaFX FXML 中所述。

但是,您也可以使用嵌套事件循环:Stage.showAndWait 会阻塞,直到 Stage 关闭,因此您可以使用它来查询输入结果,并且只需准备结果并单击按钮关闭窗口。以下示例不使用 fxml,而是在使用您用来加载的 FXMLLoader 实例的实例方法加载 fxml 后,使用 FXMLLoader.getController 来获取 Controller fxml 可以让您与加载的场景进行通信。 (链接的问题包含一些很好的答案,基本上向您展示了如何做到这一点。)

@Override
public void start(Stage stage) throws IOException {
    ListView<String> listView = new ListView<>();
    Button button = new Button("Add item");

    button.setOnAction(evt -> {
        // create scene for inputing string
        TextField textField = new TextField();
        Button ok = new Button("OK");

        Scene dialogScene = new Scene(new VBox(textField, ok));
        Stage dialogStage = new Stage();
        dialogStage.setScene(dialogScene);

        // make sure the user cannot interact with original stage while the new one is opened
        dialogStage.initOwner(stage);

        // very simple way of storing the return value; replace with something better
        String[] resultContainer = new String[1];

        ok.setOnAction(e -> {
            // store dialog result & info that the user closed the window using the button
            resultContainer[0] = textField.getText();

            dialogStage.close();
        });

        // show window and wait for user to finish the input
        dialogStage.showAndWait();

        String result = resultContainer[0];
        if (result != null) {
            // deal with user input
            listView.getItems().add(result);
        }
    });

    Scene scene = new Scene(new VBox(listView, button));

    stage.setScene(scene);
    stage.show();
}

请注意,Dialog 类可能值得考虑,因为它包含直接从其 showAndWait 方法获取结果的功能,但您可能不希望仅限于使用一个DialogPane...

关于java - 如何通过关闭一个阶段并返回到上一个阶段的值而不打开一个新阶段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59009198/

相关文章:

JavaFX在禁用文本编辑时保持滚动事件激活

Java,无法在Windows上删除文件

java - 如何创建弹出消息来提醒用户字段不完整?

java - 与展望整合

java - 无法使用 Jackson 解码 LocalDate 和 LocalTime 类

java - 如何从 Map 映射到 List

java - 使用 Gradle 的重复类输出 jar

java - 使用监听器将 TextField 的内容添加到列表中

java - java中的集合可以在场景生成器中消失

java - javafx中的部分透明图像