笔记记录

编写人:老王
时间:2023-04-10
地点:广州


BorderPane就是方位布局

分为上下左右和中间。5个地方放什么东西都可以,非常nice的一个布局。
图片.png

  1. package com.example.demo2;
  2. import javafx.application.Application;
  3. import javafx.scene.Scene;
  4. import javafx.scene.layout.AnchorPane;
  5. import javafx.scene.layout.BorderPane;
  6. import javafx.stage.Stage;
  7. public class Main3 extends Application {
  8. public static void main(String[] args) {
  9. launch(args);
  10. }
  11. @Override
  12. public void start(Stage stage) throws Exception {
  13. AnchorPane a1 = new AnchorPane();
  14. //橙色
  15. a1.setStyle("-fx-background-color: #ff7f50;");
  16. a1.setPrefHeight(100);
  17. a1.setPrefWidth(100);
  18. AnchorPane a2 = new AnchorPane();
  19. //蓝色
  20. a2.setStyle("-fx-background-color: #00ffff;");
  21. a2.setPrefHeight(100);
  22. a2.setPrefWidth(100);
  23. AnchorPane a3 = new AnchorPane();
  24. //粉色
  25. a3.setStyle("-fx-background-color: #ff1493;");
  26. a3.setPrefHeight(100);
  27. a3.setPrefWidth(100);
  28. AnchorPane a4 = new AnchorPane();
  29. //绿色
  30. a4.setStyle("-fx-background-color: #90ee90;");
  31. a4.setPrefHeight(100);
  32. a4.setPrefWidth(100);
  33. AnchorPane a5 = new AnchorPane();
  34. //红色
  35. a5.setStyle("-fx-background-color: #ff0000;");
  36. a5.setPrefHeight(100);
  37. a5.setPrefWidth(100);
  38. BorderPane bor =new BorderPane();
  39. //黄色
  40. bor.setStyle("-fx-background-color: #ffff00;");
  41. bor.setTop(a1);
  42. bor.setBottom(a2);
  43. bor.setLeft(a3);
  44. bor.setRight(a4);
  45. bor.setCenter(a5);
  46. Scene scene = new Scene(bor);
  47. stage.setScene(scene);
  48. stage.setHeight(800);
  49. stage.setWidth(900);
  50. stage.show();
  51. }
  52. }

图片.png设置方位布局的内边距为10:

//黄色
        bor.setStyle("-fx-background-color: #ffff00;");
        bor.setPadding(new Insets(10));
        bor.setTop(a1);
        bor.setBottom(a2);
        bor.setLeft(a3);
        bor.setRight(a4);
        bor.setCenter(a5);

图片.png设置顶部的外边距:

bor.setMargin(a1,new Insets(10));

图片.png