【发布时间】:2016-09-03 03:38:39
【问题描述】:
基本上,我有一个弹出式显示,其中有一个文本字段,我希望用户在其中输入一个名称(然后我最终得到该名称并稍后使用它,但这没关系)。不幸的是,文本字段中的文本没有更新(从图形上看,程序仍然可以获取通过 tf.getText() 输入的内容,但我看不到文本更新)。
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle("Naming");
window.setMinWidth(300);
window.setMinHeight(200);
Label label = new Label();
label.setText("Please type a name");
Button submitButton = new Button("Submit");
TextField tf = new TextField();
tf.setText("Please enter a name");
tf.setMaxWidth(200);
submitButton.setOnAction(e ->{
System.out.println(tf.getText());
window.close();
});
VBox layout = new VBox(10);
layout.getChildren().addAll(label, submitButton, tf);
Scene scene = new Scene(layout);
window.setScene(scene);
window.showAndWait();
这个问题可以通过将window.showAndWait(); 更改为window.show() 来解决,但我想知道它是否也可以通过其他方式解决。
【问题讨论】:
-
您提供的代码运行良好,尽管需要信息。您使用什么 Java 版本?您是否将上述代码调用到 JavaFX 线程中?您是否正在运行任何使应用程序滞后的 Lock 或 Thread.sleep() 方法?