JavaFx 对话框删除底部 space

JavaFx Dialog remove bottom space

考虑以下代码:

public class Main extends Application {
    public static void main(String[] args) throws Exception {
        launch( args );
    }

    @Override
    public void start(Stage stage) throws Exception {
        final BorderPane p = new BorderPane();

        final Dialog dialog = new Dialog() { {
            getDialogPane().setContent( new Button( "TEXT" ) );
        } };

        final Button bt = new Button( "LAUNCH" );
        bt.setOnAction( e -> dialog.show() );

        p.setCenter( bt );

        stage.setScene( new Scene( p ));

        stage.show();
    }
}

结果是:

代码是简化示例。

底部的额外 space 有问题。有什么办法可以去除吗?谢谢。

你可以这样做:

@Override
public void start(Stage stage)
{
    final BorderPane p = new BorderPane();

    Stage dialog = new Stage();
    dialog.initModality(Modality.APPLICATION_MODAL);
    dialog.setScene(new Scene(new Button("TEXT")));               

    final Button bt = new Button( "LAUNCH" );
    bt.setOnAction( e -> dialog.show() );

    p.setCenter( bt );

    stage.setScene( new Scene( p ));

    stage.show();
}

使用此选择器在与 class 相同的包中定义样式文件 "style.css":

.dialog-pane > .button-bar > .container {
    -fx-padding: 0;
}

在 Java 代码中设置样式 sheet:


dialog.getDialogPane().getStylesheets().add(getClass().getResource("style.css").toExternalForm());

最简单的方法:

dialog.getDialogPane().setPadding(Insets.EMPTY);