[英]How I can set the buttons on the center of the scene?
如何將按鈕垂直和水平居中?
無論我使用什么窗格或 Vbox,我都無法讓它工作。
這是我的舞台:
public void start(Stage stage) {
BorderPane bp = new BorderPane();
bp.setLeft(createLeftArea());
bp.setRight(createRightArea());
bp.setStyle("-fx-padding: 10 35 50 35;");
bp.setCenter(createCenterArea());
var scene = new Scene(new StackPane(bp), 640, 480);
stage.setScene(scene);
stage.getIcons().add(new Image("https://mpng.subpng.com/20180404/ebw/kisspng-java-programming-computer-programming-programming-coffee-jar-5ac598db779939.2171835915228991634899.jpg"));
stage.setTitle("Lists");
stage.show();
}
這是我創建按鈕的地方:
public GridPane createCenterArea(){
Button b1 = new Button("->");
Button b2 = new Button("<-");
b1.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
b2.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
TilePane tileButtons = new TilePane(Orientation.HORIZONTAL);
tileButtons.setPadding(new Insets(20, 10, 20, 0));
tileButtons.setHgap(10.0);
tileButtons.setVgap(4.0);
tileButtons.getChildren().addAll(b1, b2);
GridPane gpbnt = new GridPane();
gpbnt.add(tileButtons, 2, 0);
return gpbnt;
}
你對我做錯了什么有任何想法嗎? 先感謝您!
我自己解決了。 我使用了 VBox 和 SetAlignment 方法。
這就是它現在的樣子: https://i.stack.imgur.com/ExtaE.png
這是按鈕代碼:
private VBox createCenterButtons(){
Button b1 = new Button();
Button b2 = new Button();
b1.setPrefSize(45, 35);
b2.setPrefSize(45, 35);
VBox vb = new VBox(b1,b2);
vb.setSpacing(5.0);
vb.setAlignment(Pos.CENTER_LEFT);
vb.setPadding(new Insets(0, 10, 0, 10));
return vb;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.