[英]How to put two buttons in each corner in the bottom of a borderpane
我試圖放置兩個不同的按鈕,一個在左下角,另一個在右下角。 這兩個按鈕是在型動物HBox
和兩個橫向方框都在底部Borderpane
為此,我這樣做了,它可以工作,但很難看:
envoisButton = new Button("Envoyer la commande");
envoisButton.setStyle("-fx-font: 20px \"Roboto\";");
envoisButton.setPrefSize(300,50);
envoisButton.setGraphic(new ImageView(new Image("PleinVide/images/OK.png")));
HBox.setMargin(envoisButton,new Insets(0,20,20,20));
HBox rightButtons = new HBox(envoisButton);
rightButtons.setAlignment(Pos.CENTER_RIGHT);
synchroButton = new Button("Synchroniser");
synchroButton.setStyle("-fx-font: 20px \"Roboto\";");
synchroButton.setPrefSize(300,50);
synchroButton.setGraphic(new ImageView(new Image("PleinVide/images/synchronize.png")));
HBox.setMargin(synchroButton,new Insets(0,20,20,20));
HBox leftButtons = new HBox(synchroButton);
leftButtons.setAlignment(Pos.CENTER_LEFT);
HBox buttons = new HBox(leftButtons,rightButtons);
buttons.setSpacing(primaryScreenBounds.getWidth()*0.65); //This is very ugly (primaryScreenBounds == my screen resolution)
borderPane.setBottom(buttons);
結果如下: 我們可以看到這 2 個按鈕是我想要的位置,但是如果將來我想添加另一個按鈕,這根本不起作用:/
你有沒有辦法把兩個按鈕放在角落里? 非常感謝。
將兩個HBox
包裹在另一個HBox
,設置適當的對齊方式,並使它們始終水平增長。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane(new Label("Hello, World!"));
HBox leftBox = new HBox(new Button("Left Button"));
leftBox.setAlignment(Pos.CENTER_LEFT);
HBox.setHgrow(leftBox, Priority.ALWAYS);
HBox rightBox = new HBox(new Button("Right Button"));
rightBox.setAlignment(Pos.CENTER_RIGHT);
HBox.setHgrow(rightBox, Priority.ALWAYS);
HBox bottom = new HBox(leftBox, rightBox);
bottom.setPadding(new Insets(10));
root.setBottom(bottom);
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.show();
}
}
另一種選擇是使用單個HBox
並使用“填充”區域/窗格。 例如:
HBox hbox = new HBox();
hbox.getChildren().add(new Button("Left Button"));
Pane filler = new Pane();
hbox.getChildren().add(filler);
HBox.setHgrow(filler, Priority.ALWAYS);
hbox.getChildren().add(new Button("Right Button"));
borderPane.setBottom(hbox);
我會建議
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<BorderPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<center>
<Pane style="-fx-background-color: red;" BorderPane.alignment="CENTER" />
</center>
<bottom>
<AnchorPane maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" style="-fx-background-color: green;" BorderPane.alignment="CENTER">
<children>
<Button text="Button" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="20.0" AnchorPane.topAnchor="20.0" />
<Button text="Button" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="20.0" />
</children>
</AnchorPane>
</bottom>
</BorderPane>
你可以使用網格板。
要么在您的示例中添加按鈕,要么添加一個網格窗格,然后在該網格窗格中添加按鈕。
或者
使用網格窗格作為您的場景和樣式並相應地添加約束(這更困難)。 這樣,如果您想添加按鈕,您需要做的就是找出網格窗格中插槽的坐標。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.