【发布时间】:2020-03-06 06:31:56
【问题描述】:
我有一个线程在我的子阶段内每 5 秒运行一次。当我打开和关闭子阶段时,该线程将继续运行。它应该在单击关闭按钮时删除所有孩子的舞台事件和内容(屏幕右上角的十字)。我正在子阶段内基于 TEXT 打印文本。
它打印为
非空
这意味着节点仍然存在于屏幕上。会影响应用 性能,因为我需要经常打开和关闭这些子阶段。 请指教,这是我的代码。
主类:
public class dashboard extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane();
Button btn = new Button("Open child window");
btn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
final Stage dialog = new Stage();
dialog.setTitle("Sensors Assignment");
dialog.initModality(Modality.WINDOW_MODAL);
dialog.initOwner(primaryStage);
dialog.setResizable(false);
VBox dialogVbox = (new childWindowClass()).GetChildContent();
//dialogVbox.getChildren().add(closeButton);
Scene dialogScene = new Scene(dialogVbox);
dialog.setScene(dialogScene);
dialog.showAndWait();
dialog.setOnCloseRequest(e -> {System.out.println("Stage is closing");dialog.close();});
}
});
pane.setCenter(new VBox(new Text("Test 1234"), btn));
ScrollPane scrollPane = new ScrollPane(pane);
scrollPane.setFitToWidth(true);
scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
Scene scene = new Scene(scrollPane);
primaryStage.setTitle("test");
primaryStage.setMaximized(true);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
儿童班:
public class childWindowClass {
private Text txt; private int _index = 0; SimpleDateFormat dateFrmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
public VBox GetChildContent() {
txt = new Text("Child Window Test " + _index);
Label refreshClock = new Label(dateFrmt.format(new Date()));
Thread timerThread = new Thread(() -> {
while (true) {
try { Thread.sleep(5000); }
catch (InterruptedException e) {e.printStackTrace();}
Platform.runLater(() -> {
refreshClock.setText(dateFrmt.format(new Date()));
try {
System.out.println(dateFrmt.format(new Date()) + " - " + (txt == null ? "" : " not") + " NULL");
if(txt != null)
txt.setText("Child Window Test " + _index);
_index++;
}
catch (Exception e) {e.printStackTrace();}
});
}
});
timerThread.start();
return new VBox(txt, refreshClock);
}
}
【问题讨论】:
-
与您的问题无关:请学习 java 命名约定并遵守它们。
标签: java javafx parent-child