我在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();
@James_D评论确实是正确的。如果你看看BorderPane documentation,它描述了一个BorderPane“在顶部,左侧,右侧,底部和中心位置布置儿童。”
因此,就像您将menuBar设置为使用root.setTop(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);
}
}