Image.pngAnchorPane:绝对布局

  1. @Override
  2. public void start(Stage primaryStage) throws Exception{
  3. TextArea textArea = new TextArea();
  4. AnchorPane ap = new AnchorPane();
  5. AnchorPane.setTopAnchor(textArea,10.0);
  6. AnchorPane.setBottomAnchor(textArea,10.0);
  7. AnchorPane.setLeftAnchor(textArea,10.0);
  8. AnchorPane.setRightAnchor(textArea,10.0);
  9. //这样设置的话,textArea会居中显示,并随着ap的放大而放大。
  10. ap.getChildren().add(textArea);
  11. Scene scene = new Scene(ap);
  12. primaryStage.setScene(scene);
  13. primaryStage.setTitle("标题栏");
  14. primaryStage.setWidth(400);
  15. primaryStage.setHeight(400);
  16. primaryStage.show();//启动
  17. }

HBox及VBox:线性布局(常用于菜单栏)

  1. @Override
  2. public void start(Stage primaryStage) throws Exception{
  3. Button btn = new Button("Button1");
  4. Button btn2 = new Button("Button2");
  5. Button btn3 = new Button("Button3");
  6. AnchorPane ap = new AnchorPane();
  7. HBox box = new HBox();//横向线性布局
  8. hbox.setAlignment(Pos.CENTER)//设置子控件居中
  9. box.setPrefHeight(400);
  10. box.setPrefWidth(400);
  11. box.setSpacing(10);//设置子控件的间距
  12. box.getChildren().addAll(btn,btn2,btn3);
  13. ap.getChildren().add(box);
  14. VBox vbox = new VBox();//纵向线性布局
  15. Scene scene = new Scene(ap);
  16. primaryStage.setScene(scene);
  17. primaryStage.setTitle("标题栏");
  18. primaryStage.setWidth(400);
  19. primaryStage.setHeight(400);
  20. primaryStage.show();//启动
  21. }

image.png