在Javafx中如何对齐对话框的按钮? [英] How To Align Ok Button Of A Dialog Pane In Javafx?

查看:505
本文介绍了在Javafx中如何对齐对话框的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对齐,即将对中的CENTER放在对话框的确定按钮上。我已经尝试了下面的代码,但它不工作。

 对话框对话框=新的Dialog(); 
DialogPane dialogPane = dialog.getDialogPane();
dialogPane.setStyle( - fx-background-color:#fff;);

//设置按钮类型。
ButtonType okButtonType = new ButtonType(Ok,ButtonBar.ButtonData.OK_DONE);
ButtonType cancelButtonType = new ButtonType(Cancel,ButtonBar.ButtonData.CANCEL_CLOSE);
dialog.getDialogPane()。getButtonTypes()。addAll(okButtonType,cancelButtonType);
dialogPane.lookupButton(cancelButtonType).setVisible(false);

//测试
按钮okButton =(Button)dialog.getDialogPane()。lookupButton(okButtonType);
okButton.setAlignment(Pos.CENTER);
//结束测试

dialog.showAndWait();


解决方案

对话框的ButtonBar中的居中按钮实际上是令人惊讶的很难以非黑客的方式实现。



以下是我可以想出的最好的解决方案。它依赖于

  import javafx.application.Application; 
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control。*;
import javafx.scene.layout。*;
import javafx.stage.Stage;

import java.util.Optional;

public class CenteredDialogBu​​ttons extends Application {
@Override
public void start(Stage stage){
Button show = new Button(Show Dialog);

Dialog< ButtonType> dialog = new Dialog<>();
DialogPane dialogPane = new DialogPane(){
@Override
protected Node createButtonBar(){
ButtonBar buttonBar =(ButtonBar)super.createButtonBar();
buttonBar.setButtonOrder(ButtonBar.BUTTON_ORDER_NONE);

return buttonBar;
}
};
dialog.setDialogPane(dialogPane);
dialogPane.getButtonTypes()。addAll(ButtonType.OK);
dialogPane.setContentText(Centered Button);

Region spacer = new Region();
ButtonBar.setButtonData(spacer,ButtonBar.ButtonData.BIG_GAP);
HBox.setHgrow(spacer,Priority.ALWAYS);
dialogPane.applyCss();
HBox hbox =(HBox)dialogPane.lookup(。container);
hbox.getChildren()。add(spacer);

show.setOnAction(e - > {
可选< ButtonType> result = dialog.showAndWait();
if(result.isPresent()&& get()== ButtonType.OK){
System.out.println(OK);
}
});

StackPane layout = new StackPane(
show
);
layout.setPadding(new Insets(50));

场景场景=新场景(布局);

stage.setScene(scene);
stage.show();
}

public static void main(String [] args){
launch(args);
}
}

我不太喜欢这个解决方案的原因是动态CSS查询类型违反API封装,因为控件(如按钮栏)的JavaFX场景图的CSS结构并不是其公共API的一部分。但是,我不认为使用JavaFX 8的现有公共API和默认的ButtonBar外观可以在ButtonBar中使用居中的按钮。



另一种方法将为与对话框相关联的ButtonBar创建一个自定义皮肤,但是这种方法是相当困难的,我不会为此任务推荐它。



基本上,所有这一切的外卖,只要离开默认的按钮布局,并可以随时订购对话框,而不是尝试自定义对话框按钮布局。如果您确实希望将完全自定义的布局设置为按钮放置的级别,那么您可以通过对Stage进行子类化来创建自己的自定义对话框类,而不是将自定义对话框实现基于内置对话框类。 / p>

相关,但稍微不同的信息在:




I want to align i.e Position CENTER an OK button of a DialogPane. I have tried the below code but its not working.

Dialog dialog = new Dialog();
DialogPane dialogPane = dialog.getDialogPane();
dialogPane.setStyle("-fx-background-color: #fff;");

        // Set the button types.
ButtonType okButtonType = new ButtonType("Ok", ButtonBar.ButtonData.OK_DONE);
ButtonType cancelButtonType = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
dialog.getDialogPane().getButtonTypes().addAll(okButtonType, cancelButtonType);
dialogPane.lookupButton(cancelButtonType).setVisible(false);

        // Testing
Button okButton = (Button) dialog.getDialogPane().lookupButton(okButtonType);
okButton.setAlignment(Pos.CENTER);
        // End Testing

dialog.showAndWait();

解决方案

Centering buttons in the ButtonBar of a Dialog is actually surprisingly difficult to achieve in a non-hacky way.

Below is the best solution I could come up with. It relies upon a dynamic CSS lookup of the HBox for the button container, to which it then adds a spacer region on the right to push the buttons to the left (the default ButtonSkin implementation already places an implicit spacer of the left which pushes the buttons to the right, which I determined using ScenicView). The combination of the left and right spacers end up aligning the buttons in the center. The solution also overrides the ButtonBar creation to stop the ButtonSkin internally reordering and performing additional layout of buttons, as, when it does that, you can't really reliably customize the layout yourself.

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import java.util.Optional;

public class CenteredDialogButtons extends Application {
    @Override
    public void start(Stage stage) {
        Button show = new Button("Show Dialog");

        Dialog<ButtonType> dialog = new Dialog<>();
        DialogPane dialogPane = new DialogPane() {
            @Override
            protected Node createButtonBar() {
                ButtonBar buttonBar = (ButtonBar) super.createButtonBar();
                buttonBar.setButtonOrder(ButtonBar.BUTTON_ORDER_NONE);

                return buttonBar;
            }
        };
        dialog.setDialogPane(dialogPane);
        dialogPane.getButtonTypes().addAll(ButtonType.OK);
        dialogPane.setContentText("Centered Button");

        Region spacer = new Region();
        ButtonBar.setButtonData(spacer, ButtonBar.ButtonData.BIG_GAP);
        HBox.setHgrow(spacer, Priority.ALWAYS);
        dialogPane.applyCss();
        HBox hbox = (HBox) dialogPane.lookup(".container");
        hbox.getChildren().add(spacer);

        show.setOnAction(e -> {
            Optional<ButtonType> result = dialog.showAndWait();
            if (result.isPresent() && result.get() == ButtonType.OK) {
                System.out.println("OK");
            }
        });

        StackPane layout = new StackPane(
                show
        );
        layout.setPadding(new Insets(50));

        Scene scene = new Scene(layout);

        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

The reason I don't quite like this solution is that the dynamic CSS lookups kind of violate API encapsulation, as the CSS structure of the JavaFX scene graphs for controls such as button bars is not really part of their public API. However, I don't think it is really possible to get centered buttons in a ButtonBar using the existing public APIs for JavaFX 8 and a default ButtonBar skin.

An alternate approach would be to create a custom skin for the ButtonBar associated with the dialog, but that approach is quite difficult and I wouldn't recommend it for this task.

Basically, the takeaway from all this is, just leave the default button layout and order for dialogs whenever you can, rather than trying to customize the dialog button layout. If you do want to have completely customized layout to the level of things like button placement, then you may be better off just creating your own custom dialog class by subclassing Stage rather than basing your custom dialog implementation on the in-built dialog class.

Related, but slightly different information is in:

这篇关于在Javafx中如何对齐对话框的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆