【发布时间】:2014-07-10 18:56:42
【问题描述】:
我正在尝试从任务(线程)关闭 JavaFX 中的阶段。
为了实现这一点,我尝试将 Stage 的引用传递给扩展 Task 的类,在那里设置当前 Stage。
然后在 call() 结束时关闭舞台。但是 .close() 和 .hide() 根本没有隐藏/关闭舞台。
类:SampleStage
public class SampleStage extends Application {
@Override
public void start(final Stage primaryStage) throws Exception {
primaryStage.setTitle("JavaFx Dialog");
final Button btn = new Button();
btn.setText("Click me to display popup dialog");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
Stage dialog = new Stage();
Taskee task = new Taskee();
dialog.initStyle(StageStyle.UTILITY);
task.setStage(dialog);
new Thread(task).start();
Scene scene2 = new Scene(new Group(new Text(25, 25, "Hello World!")));
dialog.setScene(scene2);
dialog.show();
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
班级任务对象:
导入 javafx.concurrent.Task; 导入javafx.stage.Stage;
public class Taskee extends Task<Void>{
private Stage stage;
@Override
protected Void call() throws Exception {
for(int i=0;i<10;i++){
//@DoSomething()
for(long l=0;l<10000;l++);
System.out.println("i=" + i);
}
getStage().close();
getStage().hide();
return null;
}
public Stage getStage() {
return stage;
}
public void setStage(Stage stage) {
this.stage = stage;
}
}
注意:getStage().getScene().getWindow().hide(); 也不起作用。
【问题讨论】:
标签: java user-interface javafx javafx-2 task