在JavaFX中关闭对话框

时间:2013-04-18 12:19:16

标签: java javafx-2 javafx

我在JavaFX中有这个简单的对话框:

Stage dialogStage = new Stage();
        dialogStage.initModality(Modality.WINDOW_MODAL);
        dialogStage.setScene(new Scene(VBoxBuilder.create()
                .children(new Text(text), new Button("Close")).alignment(Pos.CENTER).padding(new Insets(5)).build(), xSize, ySize, backgroundColor));
        dialogStage.show();

我想在点击Close按钮时关闭对话框,以便我这样修改代码:

Stage dialogStage = new Stage();
        dialogStage.initModality(Modality.WINDOW_MODAL);
        dialogStage.setScene(new Scene(VBoxBuilder.create()
                .children(new Text(text), new Button("Close").setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                aboutDialog();
            }
        })).alignment(Pos.CENTER).padding(new Insets(5)).build(), xSize, ySize, backgroundColor));
        dialogStage.show();

但是我在Netbeans中收到此错误消息:'void' type not allowed here

您能告诉他们使用Close按钮关闭对话框的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

而不是

@Override
public void handle(ActionEvent e) {
  aboutDialog();
}

@Override
public void handle(ActionEvent e) {
  dialogStage.hide();
}

答案 1 :(得分:0)

您有'void' type not allowed here,因为当您调用setOnAction方法时,它会返回void个对象。要避免此问题,您需要在构建器外部安装处理程序,如下所示:

Button but = new Button("Close");
but.setOnAction(new EventHandler<ActionEvent>() {
   @Override
   public void handle(ActionEvent e) {
      aboutDialog();
   }
});

Stage dialogStage = new Stage();
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.setScene(new Scene(VBoxBuilder.create()
                .children(new Text(text), but).alignment(Pos.CENTER).padding(new Insets(5)).build(), xSize, ySize, backgroundColor));
dialogStage.show();