03LOM@M]57%N2(QIAY28A9C.png
BorderPane布局顶部,底部,左,右或中心区域中的子节点。每个区域只能有一个节点。BorderPane的顶部和底部区域允许可调整大小的节点占用所有可用宽度。
左边界区域和右边界区域占据顶部和底部边界之间的可用垂直空间。
默认情况下,所有边界区域尊重子节点的首选宽度和高度。放置在顶部,底部,左侧,右侧和中心区域中的节点的默认对齐方式如下:

  • 顶部: Pos.TOP_LEFT
  • 底部: Pos.BOTTOM_LEFT
  • 左侧: Pos.TOP_LEFT
  • 右侧: Pos.TOP_RIGHT
  • 中心: Pos.CENTER

    示例1

    将按钮添加到BorderPane,如下代码所示 ```java import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.BorderPane; import javafx.stage.Stage;

public class Main extends Application { public static void main(String[] args) { Application.launch(args); }

  1. @Override
  2. public void start(Stage primaryStage) {
  3. primaryStage.setTitle("BorderPane Test");
  4. BorderPane bp = new BorderPane();
  5. //bp.setPadding(new Insets(10, 20, 10, 20));
  6. Button btnTop = new Button("Top");
  7. bp.setTop(btnTop);
  8. Button btnLeft = new Button("Left");
  9. bp.setLeft(btnLeft);
  10. Button btnCenter = new Button("Center");
  11. bp.setCenter(btnCenter);
  12. Button btnRight = new Button("Right");
  13. bp.setRight(btnRight);
  14. Button btnBottom = new Button("Bottom");
  15. bp.setBottom(btnBottom);
  16. Scene scene = new Scene(bp, 300, 200);
  17. primaryStage.setScene(scene);
  18. primaryStage.show();
  19. }

}

  1. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23145762/1650305013053-2b025449-1d16-47a0-8282-e0d12a82f55b.png#clientId=ube513240-8bbf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=231&id=u8b795fa8&margin=%5Bobject%20Object%5D&name=image.png&originHeight=289&originWidth=377&originalType=binary&ratio=1&rotation=0&showTitle=false&size=8759&status=done&style=shadow&taskId=u8bec5b19-3212-4bf2-969a-9882eb5622f&title=&width=301.6)
  2. <a name="JLaMZ"></a>
  3. ## 示例2
  4. ```java
  5. package com.javafx03;
  6. import javafx.application.Application;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.Button;
  9. import javafx.scene.layout.Background;
  10. import javafx.scene.layout.BorderPane;
  11. import javafx.scene.paint.Color;
  12. import javafx.stage.Stage;
  13. public class JavaFx07 extends Application {
  14. @Override
  15. public void start(Stage stage) {
  16. BorderPane borderPane = new BorderPane();
  17. borderPane.setBackground(Background.fill(Color.GRAY));
  18. borderPane.setTop(new Button("TOP"));
  19. borderPane.setLeft(new Button("LEFT"));
  20. borderPane.setRight(new Button("RIGHT"));
  21. borderPane.setCenter(new Button("Center"));
  22. borderPane.setBottom(new Button("Bottom"));
  23. Scene scene = new Scene(borderPane,300,300);
  24. stage.setScene(scene);
  25. stage.show();
  26. }
  27. public static void main(String[] args) {
  28. launch(args);
  29. }
  30. }

image.png

复杂布局

  1. package com.javafx03;
  2. import javafx.application.Application;
  3. import javafx.geometry.Insets;
  4. import javafx.geometry.Pos;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Button;
  7. import javafx.scene.control.Hyperlink;
  8. import javafx.scene.layout.*;
  9. import javafx.scene.paint.Color;
  10. import javafx.scene.text.Font;
  11. import javafx.scene.text.FontWeight;
  12. import javafx.scene.text.Text;
  13. import javafx.stage.Stage;
  14. public class JavaFx08 extends Application {
  15. @Override
  16. public void start(Stage stage) {
  17. BorderPane borderPane = new BorderPane();
  18. borderPane.setBackground(Background.fill(Color.GRAY));
  19. HBox top = new HBox();
  20. top.setBackground(Background.fill(Color.BLUE));
  21. top.setMinHeight(60);
  22. Text text = new Text("Welcome 进销存");
  23. text.setFont(Font.font("宋体", FontWeight.BOLD,20));
  24. top.setAlignment(Pos.CENTER);
  25. top.getChildren().add(text);
  26. borderPane.setTop(top);
  27. VBox left = new VBox(10);
  28. left.setPadding(new Insets(10));
  29. left.setBackground(Background.fill(Color.PINK));
  30. left.setMinWidth(100);
  31. Button system = new Button("系统设置");
  32. left.getChildren().addAll(system,new Button("商品管理"),new Button("关于我们"),new Button("联系我们"));
  33. borderPane.setLeft(left);
  34. GridPane gridPane = new GridPane();
  35. gridPane.setBackground(Background.fill(Color.RED));
  36. gridPane.setMinWidth(400);
  37. gridPane.setMinHeight(240);
  38. borderPane.setCenter(gridPane);
  39. //borderPane.setRight(new Button("RIGHT"));
  40. system.setOnAction(e->{
  41. gridPane.setBackground(Background.fill(Color.BLACK));
  42. });
  43. HBox buttom = new HBox(10);
  44. buttom.setPadding(new Insets(10));
  45. buttom.setAlignment(Pos.CENTER);
  46. buttom.getChildren().addAll(new Button("系统设置"),new Button("商品管理"),new Button("关于我们"),new Button("联系我们"));
  47. borderPane.setBottom(buttom);
  48. Scene scene = new Scene(borderPane,600,400);
  49. stage.setScene(scene);
  50. stage.show();
  51. }
  52. public static void main(String[] args) {
  53. launch(args);
  54. }
  55. }

Video_2022-04-27_004131.wmv (197.48KB)

实例 - 菜单导航栏宽度是屏幕宽度

使用场景绑定BorderPane宽度和高度

  1. import javafx.application.Application;
  2. import javafx.event.ActionEvent;
  3. import javafx.event.EventHandler;
  4. import javafx.scene.Group;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Menu;
  7. import javafx.scene.control.MenuBar;
  8. import javafx.scene.control.MenuItem;
  9. import javafx.scene.layout.BorderPane;
  10. import javafx.scene.paint.Color;
  11. import javafx.stage.Stage;
  12. public class Main extends Application {
  13. public static void main(String[] args) {
  14. Application.launch(args);
  15. }
  16. @Override
  17. public void start(Stage primaryStage) {
  18. primaryStage.setTitle("Title");
  19. Group root = new Group();
  20. Scene scene = new Scene(root, 400, 250, Color.WHITE);
  21. MenuBar menuBar = new MenuBar();
  22. EventHandler<ActionEvent> action = changeTabPlacement();
  23. Menu menu = new Menu("Direction");
  24. MenuItem left = new MenuItem("Left");
  25. left.setOnAction(action);
  26. menu.getItems().add(left);
  27. MenuItem right = new MenuItem("Right");
  28. right.setOnAction(action);
  29. menu.getItems().add(right);
  30. MenuItem top = new MenuItem("Top");
  31. top.setOnAction(action);
  32. menu.getItems().add(top);
  33. MenuItem bottom = new MenuItem("Bottom");
  34. bottom.setOnAction(action);
  35. menu.getItems().add(bottom);
  36. menuBar.getMenus().add(menu);
  37. BorderPane borderPane = new BorderPane();
  38. borderPane.prefHeightProperty().bind(scene.heightProperty());
  39. borderPane.prefWidthProperty().bind(scene.widthProperty());
  40. borderPane.setTop(menuBar);
  41. root.getChildren().add(borderPane);
  42. primaryStage.setScene(scene);
  43. primaryStage.show();
  44. }
  45. private EventHandler<ActionEvent> changeTabPlacement() {
  46. return new EventHandler<ActionEvent>() {
  47. public void handle(ActionEvent event) {
  48. MenuItem mItem = (MenuItem) event.getSource();
  49. String side = mItem.getText();
  50. if ("left".equalsIgnoreCase(side)) {
  51. System.out.println("left");
  52. } else if ("right".equalsIgnoreCase(side)) {
  53. System.out.println("right");
  54. } else if ("top".equalsIgnoreCase(side)) {
  55. System.out.println("top");
  56. } else if ("bottom".equalsIgnoreCase(side)) {
  57. System.out.println("bottom");
  58. }
  59. }
  60. };
  61. }
  62. }

image.png