网格布局
GridPane通常用于布局:表单布局
GridPane可以在行,列或单元格级别指定约束。
例如,我们可以设置包含输入文本字段的第二列,以在窗口调整大小时调整大小。

使用Java FX创建表格的时候,这个布局非常方便。

包javafx.scene.layout名为GridPane这个类提供了 11 个属性,它们是 -

  • alignment- 此属性可以设置位置,使用方式setAlignment()方法设置此属性的值。

放置在顶部,底部,左侧,右侧和中心区域中的节点的默认对齐方式如下:

  1. - 顶部: Pos.TOP_LEFT
  2. - 底部: Pos.BOTTOM_LEFT
  3. - 左侧: Pos.TOP_LEFT
  4. - 右侧: Pos.TOP_RIGHT
  5. - 中心: Pos.CENTER
  • hgap- 此属性的类型为double,表示列之间的水平差距。
  • vgap- 属性的类型为double,它表示行之间的垂直间距。
  • gridLinesVisible- 此属性是布尔类型,显示表格线
  1. import javafx.application.Application;
  2. import javafx.geometry.HPos;
  3. import javafx.geometry.Insets;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.control.Label;
  7. import javafx.scene.control.TextField;
  8. import javafx.scene.layout.BorderPane;
  9. import javafx.scene.layout.ColumnConstraints;
  10. import javafx.scene.layout.GridPane;
  11. import javafx.scene.layout.Priority;
  12. import javafx.scene.paint.Color;
  13. import javafx.stage.Stage;
  14. public class Main extends Application {
  15. public static void main(String[] args) {
  16. Application.launch(args);
  17. }
  18. @Override
  19. public void start(Stage primaryStage) {
  20. BorderPane root = new BorderPane();
  21. Scene scene = new Scene(root, 380, 300, Color.WHITE);
  22. GridPane gridpane = new GridPane();
  23. //表格实线
  24. gridpane.setGridLinesVisible(true);
  25. gridpane.setPadding(new Insets(5));
  26. //水平间距
  27. gridpane.setHgap(5);
  28. //垂直间距
  29. gridpane.setVgap(50);
  30. ColumnConstraints column1 = new ColumnConstraints(100);
  31. ColumnConstraints column2 = new ColumnConstraints(50, 150, 300);
  32. column2.setHgrow(Priority.ALWAYS);
  33. gridpane.getColumnConstraints().addAll(column1, column2);
  34. Label fNameLbl = new Label("First Name");
  35. TextField fNameFld = new TextField();
  36. Label lNameLbl = new Label("Last Name");
  37. TextField lNameFld = new TextField();
  38. Button saveButt = new Button("Save");
  39. // First name label
  40. GridPane.setHalignment(fNameLbl, HPos.RIGHT);
  41. gridpane.add(fNameLbl, 0, 0);
  42. // Last name label
  43. GridPane.setHalignment(lNameLbl, HPos.RIGHT);
  44. gridpane.add(lNameLbl, 0, 1);
  45. // First name field
  46. GridPane.setHalignment(fNameFld, HPos.LEFT);
  47. gridpane.add(fNameFld, 1, 0);
  48. // Last name field
  49. GridPane.setHalignment(lNameFld, HPos.LEFT);
  50. gridpane.add(lNameFld, 1, 1);
  51. // Save button
  52. GridPane.setHalignment(saveButt, HPos.RIGHT);
  53. gridpane.add(saveButt, 1, 2);
  54. root.setCenter(gridpane);
  55. primaryStage.setScene(scene);
  56. primaryStage.show();
  57. }
  58. }

image.png

示例

以下代码演示使用GridPane布局的简单表单应用程序。它有以下布局。

  1. +------------------------+
  2. | [label ] [ field ] |
  3. | [label ] [ field ] |
  4. | [ button ] |
  5. +------------------------+

image.png
完整的代码实现如下所示 -

  1. import javafx.application.Application;
  2. import javafx.geometry.HPos;
  3. import javafx.geometry.Insets;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.control.Label;
  7. import javafx.scene.control.TextField;
  8. import javafx.scene.layout.BorderPane;
  9. import javafx.scene.layout.ColumnConstraints;
  10. import javafx.scene.layout.GridPane;
  11. import javafx.scene.layout.Priority;
  12. import javafx.scene.paint.Color;
  13. import javafx.stage.Stage;
  14. public class Main extends Application {
  15. public static void main(String[] args) {
  16. Application.launch(args);
  17. }
  18. @Override
  19. public void start(Stage primaryStage) {
  20. BorderPane root = new BorderPane();
  21. Scene scene = new Scene(root, 380, 150, Color.WHITE);
  22. GridPane gridpane = new GridPane();
  23. gridpane.setPadding(new Insets(5));
  24. gridpane.setHgap(5);
  25. gridpane.setVgap(5);
  26. ColumnConstraints column1 = new ColumnConstraints(100);
  27. ColumnConstraints column2 = new ColumnConstraints(50, 150, 300);
  28. column2.setHgrow(Priority.ALWAYS);
  29. gridpane.getColumnConstraints().addAll(column1, column2);
  30. Label fNameLbl = new Label("First Name");
  31. TextField fNameFld = new TextField();
  32. Label lNameLbl = new Label("Last Name");
  33. TextField lNameFld = new TextField();
  34. Button saveButt = new Button("Save");
  35. // First name label
  36. GridPane.setHalignment(fNameLbl, HPos.RIGHT);
  37. gridpane.add(fNameLbl, 0, 0);
  38. // Last name label
  39. GridPane.setHalignment(lNameLbl, HPos.RIGHT);
  40. gridpane.add(lNameLbl, 0, 1);
  41. // First name field
  42. GridPane.setHalignment(fNameFld, HPos.LEFT);
  43. gridpane.add(fNameFld, 1, 0);
  44. // Last name field
  45. GridPane.setHalignment(lNameFld, HPos.LEFT);
  46. gridpane.add(lNameFld, 1, 1);
  47. // Save button
  48. GridPane.setHalignment(saveButt, HPos.RIGHT);
  49. gridpane.add(saveButt, 1, 2);
  50. root.setCenter(gridpane);
  51. primaryStage.setScene(scene);
  52. primaryStage.show();
  53. }
  54. }

image.png

示例-登录

以下是一个实现登录窗口的代码 -

  1. import javafx.application.Application;
  2. import javafx.event.ActionEvent;
  3. import javafx.event.EventHandler;
  4. import javafx.geometry.Insets;
  5. import javafx.geometry.Pos;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.Button;
  8. import javafx.scene.control.Label;
  9. import javafx.scene.control.PasswordField;
  10. import javafx.scene.control.TextField;
  11. import javafx.scene.layout.GridPane;
  12. import javafx.scene.layout.HBox;
  13. import javafx.scene.paint.Color;
  14. import javafx.scene.text.Font;
  15. import javafx.scene.text.FontWeight;
  16. import javafx.scene.text.Text;
  17. import javafx.stage.Stage;
  18. public class Main extends Application {
  19. public static void main(String[] args) {
  20. launch(args);
  21. }
  22. @Override
  23. public void start(Stage stage) {
  24. //舞台名称
  25. stage.setTitle("JavaFX Welcome");
  26. GridPane grid = new GridPane();
  27. //GridPane内容居中
  28. grid.setAlignment(Pos.CENTER);
  29. //水平间距
  30. grid.setHgap(10);
  31. //垂直间距
  32. grid.setVgap(10);
  33. //填充:距离上下左右外框
  34. grid.setPadding(new Insets(25, 25, 25, 25));
  35. Text scenetitle = new Text("Welcome");
  36. scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
  37. //0行
  38. //0列
  39. //占用2列
  40. //占用1行
  41. grid.add(scenetitle, 0, 0, 2, 1);
  42. Label userName = new Label("User Name:");
  43. grid.add(userName, 0, 1);
  44. TextField userTextField = new TextField();
  45. grid.add(userTextField, 1, 1);
  46. Label pw = new Label("Password:");
  47. grid.add(pw, 0, 2);
  48. PasswordField pwBox = new PasswordField();
  49. grid.add(pwBox, 1, 2);
  50. Button btn = new Button("Sign in");
  51. //登录按钮放到水平布局HBox中方便控制左右
  52. HBox hbBtn = new HBox(10);
  53. hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
  54. hbBtn.getChildren().add(btn);
  55. grid.add(hbBtn, 1, 4);
  56. final Text actiontarget = new Text();
  57. grid.add(actiontarget, 1, 6);
  58. btn.setOnAction(new EventHandler<ActionEvent>() {
  59. @Override
  60. public void handle(ActionEvent e) {
  61. actiontarget.setFill(Color.FIREBRICK);
  62. actiontarget.setText("Sign in button pressed");
  63. }
  64. });
  65. Scene scene = new Scene(grid, 300, 275);
  66. stage.setScene(scene);
  67. stage.show();
  68. }
  69. }

image.png