gpt4 book ai didi

java - 当放入 BorderPane 时,VBOX 被推出场景

转载 作者:行者123 更新时间:2023-12-02 11:51:27 25 4
gpt4 key购买 nike

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

 //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();

最佳答案

@James_D 的评论确实是正确的。如果你看BorderPane documentation它描述了 BorderPane“将子元素布置在顶部、左侧、右侧、底部和中心位置”。

就像您使用 root.setTop(menuBar); 将 menuBar 设置为添加到 BorderPane 的顶部一样,要设置 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);
}
}

关于java - 当放入 BorderPane 时,VBOX 被推出场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47851968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com