0

我在一个 vbox 内设置了 3 个 HBox,它显示完全居中,直到我尝试将 vbox 放入borderPane 内。我试图让一个菜单穿过场景的顶部,并将我的其余标签和文本字段放在中心,除了我的 vbox 在放入边框窗格时被推到右上角。这是我的代码,提前谢谢你。

 //COURSE TITLE PANE
    HBox courseTitlePane = new HBox(30);
    courseTitlePane.getChildren().addAll(courseTitleLabel,courseTitleField);
    courseTitlePane.setAlignment(Pos.CENTER);
    //
    //COURSE NUMBER PANE
    HBox courseNumberPane = new HBox(30);
    courseNumberPane.getChildren().addAll(courseNumberLabel,courseNumberField);
    courseNumberPane.setAlignment(Pos.CENTER);
    //
    //COURSE CREDITS PANE
    HBox creditsPane = new HBox(30);
    creditsPane.getChildren().addAll(numOfCreditsLabel,numOfCreditsField);
    creditsPane.setAlignment(Pos.CENTER);
    //
    //COURSE DESCRIPTION PANE
    HBox descriptionPane = new HBox(30);
    descriptionPane.getChildren().addAll(courseDescriptionLabel,courseDescriptionField);
    descriptionPane.setAlignment(Pos.CENTER);
    //

    VBox pane = new VBox(30);
    pane.getChildren().addAll(courseTitlePane,courseNumberPane,creditsPane,descriptionPane);
    pane.setAlignment(Pos.CENTER);

    BorderPane root = new BorderPane();
    root.getChildren().addAll(pane);
    root.setTop(menuBar);

    Scene scene = new Scene(root,800,500);
    primaryStage.setScene(scene);
    primaryStage.show();
4

1 回答 1

1

@James_D 评论确实是正确的。如果您查看BorderPane 文档,它描述了 BorderPane“在顶部、左侧、右侧、底部和中心位置布置子级”。

因此,就像您将 menuBar 设置为添加到 BorderPane 的顶部一样,root.setTop(menuBar);设​​置您必须使用的 BorderPane 的中心内容root.setCenter(pane);

这是一个完整的例子:

package sample;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        HBox courseTitlePane = new HBox(30);
        Label courseTitleLabel = new Label("Course Title");
        TextField courseTitleField = new TextField();
        courseTitlePane.getChildren().addAll(courseTitleLabel,courseTitleField);
        courseTitlePane.setAlignment(Pos.CENTER);
        //
        //COURSE NUMBER PANE
        HBox courseNumberPane = new HBox(30);
        Label courseNumberLabel = new Label("Course Number");
        TextField courseNumberField = new TextField();
        courseNumberPane.getChildren().addAll(courseNumberLabel,courseNumberField);
        courseNumberPane.setAlignment(Pos.CENTER);
        //
        //COURSE CREDITS PANE
        HBox creditsPane = new HBox(30);
        Label numOfCreditsLabel = new Label("Credits");
        TextField numOfCreditsField = new TextField();
        creditsPane.getChildren().addAll(numOfCreditsLabel,numOfCreditsField);
        creditsPane.setAlignment(Pos.CENTER);
        //
        //COURSE DESCRIPTION PANE
        HBox descriptionPane = new HBox(30);
        Label courseDescriptionLabel = new Label("Course Description");
        TextField courseDescriptionField = new TextField();
        descriptionPane.getChildren().addAll(courseDescriptionLabel, courseDescriptionField);
        descriptionPane.setAlignment(Pos.CENTER);
        //

        VBox pane = new VBox(30);
        pane.getChildren().addAll(courseTitlePane,courseNumberPane,creditsPane,descriptionPane);
        pane.setAlignment(Pos.CENTER);

        final Menu menu1 = new Menu("File");
        final Menu menu2 = new Menu("Options");
        final Menu menu3 = new Menu("Help");

        BorderPane root = new BorderPane();
        MenuBar menuBar = new MenuBar();
        root.setTop(menuBar);
        menuBar.getMenus().addAll(menu1, menu2, menu3);
        root.setCenter(pane);
        primaryStage.setTitle("Some Generic Course Application");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
于 2017-12-20T23:33:17.717 回答