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

问题描述 投票:0回答:1

实际上,我希望当我单击插入按钮(在第一阶段)时,它会打开一个新阶段,在那里我要输入一个数字,然后单击插入,然后关闭第二个窗口并返回到第一个窗口,并在其中打印插入的数字链接列表。一切工作正常,除了当我在第二阶段单击插入按钮时,它不会返回到第一阶段,而是打开新阶段并向我显示插入的数字,这样,如果我添加多个数字,它只会输出我添加的最新号码。所以我想知道如何在不打开新阶段的情况下单击insert(在第二阶段)回到上一个阶段(我还没有在代码中编写导入行)。您的帮助将不胜感激。谢谢这是我的主要代码:

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

}

这是我第一阶段的控制器代码(Home):

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

}


    Here is My Code of controller of second stage(Insert Screen):

    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 ; 
    } 
    }
    ----------
    Here is my Print class:

    class Print{
        int data;

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

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


     ----------





java javafx scenebuilder
1个回答
0
投票

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

中所述。

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

@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 ...

© www.soinside.com 2019 - 2024. All rights reserved.