无法在边框窗格中居中节点

Can't center nodes within a border pane

我这辈子都想不通为什么我不能在边框窗格的某个部分中将 VBox 居中。

@Override
public void start(Stage primaryStage) throws Exception{
    BorderPane root = new BorderPane();
    Scene scene = new Scene(root, 960, 600);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Test application");
    primaryStage.setFullScreen(true);
    primaryStage.setFullScreenExitHint("Press escape to exit fullscreen");
    primaryStage.show();
    javafx.scene.image.Image icon = new Image("sample/Riverviewcrest.png");
    primaryStage.getIcons().add(icon);

    Pane paneleft = new Pane();
    Pane paneright = new Pane();
    Pane panecenter = new Pane();
    Pane panetop = new Pane();
    Pane panebottom = new Pane();

    paneleft.setPrefWidth(100);
    paneright.setPrefWidth(100);
    panetop.setPrefHeight(100);
    panebottom.setPrefHeight(100);

    panecenter.setStyle("-fx-background-color: #0053A8");

    root.setLeft(paneleft);
    root.setRight(paneright);
    root.setCenter(panecenter);
    root.setTop(panetop);
    root.setBottom(panebottom);


    Text test = new Text("Hello");
    Text test2 = new Text("Hello");
    Text test3 = new Text("Hello");
    Text test4 = new Text("Hello");
    VBox box = new VBox();
    box.getChildren().addAll(test, test2, test3, test4);
    panecenter.getChildren().add(box);
    root.setAlignment(box, Pos.CENTER);
}

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

目的是使文本在边框窗格的中心部分居中。感谢您提供的所有帮助!

paneCenter 与其他所有内容一样只是一个窗格。所以 paneCenter 居中,而不是 VBox.

您可以使用 StackPane 并像这样调整 VBox 的对齐方式:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 960, 600);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Test application");
        primaryStage.show();

        Pane paneleft = new Pane();
        Pane paneright = new Pane();
        StackPane panecenter = new StackPane();
        Pane panetop = new Pane();
        Pane panebottom = new Pane();

        paneleft.setPrefWidth(100);
        paneright.setPrefWidth(100);
        panetop.setPrefHeight(100);
        panebottom.setPrefHeight(100);

        panecenter.setStyle("-fx-background-color: #0053A8");

        root.setLeft(paneleft);
        root.setRight(paneright);
        root.setCenter(panecenter);
        root.setTop(panetop);
        root.setBottom(panebottom);


        Text test = new Text("Hello");
        Text test2 = new Text("Hello");
        Text test3 = new Text("Hello");
        Text test4 = new Text("Hello");
        VBox box = new VBox();
        box.setAlignment(Pos.CENTER);
        box.getChildren().addAll(test, test2, test3, test4);
        panecenter.getChildren().add(box);
    }

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

或者您可以删除 panecenter 并改用 VBox

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 960, 600);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Test application");
        primaryStage.show();

        Pane paneleft = new Pane();
        Pane paneright = new Pane();
        StackPane panecenter = new StackPane();
        Pane panetop = new Pane();
        Pane panebottom = new Pane();

        paneleft.setPrefWidth(100);
        paneright.setPrefWidth(100);
        panetop.setPrefHeight(100);
        panebottom.setPrefHeight(100);

        panecenter.setStyle("-fx-background-color: #0053A8");

        root.setLeft(paneleft);
        root.setRight(paneright);
        root.setTop(panetop);
        root.setBottom(panebottom);


        Text test = new Text("Hello");
        Text test2 = new Text("Hello");
        Text test3 = new Text("Hello");
        Text test4 = new Text("Hello");
        VBox box = new VBox();
        box.setAlignment(Pos.CENTER);
        box.getChildren().addAll(test, test2, test3, test4);
        root.setCenter(box);
    }

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

根本不需要panecenter。只需将 box 设置为 BorderPane 的中心 属性 并将其居中:

box.setAlignment(Pos.CENTER);
root.setCenter(box);

您也不需要 paneleftpanerightpanetoppanebottom