我希望当我单击插入按钮(在第一阶段)时,它会打开一个新阶段,我要输入一个数字,然后单击插入,然后关闭第二个窗口并返回到第一个窗口,并在其中打印插入的数字链接列表。
一切工作正常,除了当我在第二阶段单击插入按钮时,它没有回到第一阶段,而是打开了新阶段并向我显示插入的数字,这样,如果我添加多个数字,它只会输出我添加的最新号码。所以我想知道当我单击插入(第二阶段)而不打开新阶段时如何返回上一个阶段。
这是我的主要密码:
static int[][] arrayCountDown(int n){
int[][] array = new int[n][];
for( int i=0;i<n;i++){
array[i] = new int[n-i];
for(int j=0;j<n-i;j++){
array[i][j] = n-i;
}
}
return array;
}
这是我第一阶段的控制器代码(Home):
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);
}
}
这是第二阶段控制器的代码(插入屏幕):
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;
}
}
这是我的印刷课:
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 ;
}
}
答案 0 :(得分: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
...