-1

我在 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按钮关闭对话框的正确方法是什么吗?

4

2 回答 2

0

代替

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

尝试

@Override
public void handle(ActionEvent e) {
  dialogStage.hide();
}
于 2013-04-18T12:25:21.777 回答
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();
于 2013-04-18T12:31:01.767 回答