当前位置:   article > 正文

小程序示例官方源代码_GridPane示例程序源代码

小程序官方示例源码

小程序示例官方源代码

背景 ( Background )

This JavaFX example code shows how to use the GridPane layout. The JavaFX scene is made up of GridPane containing a number of Text controls. The GridPane is used to position the Text controls in a table format.

JavaFX示例代码显示如何使用GridPane布局。 JavaFX场景由包含许多Text控件的GridPaneGridPane用于以表格格式放置Text控件。

GridPane示例 ( GridPane Example )

Executive working on laptop in boardroom
Abel Mitja Varela/E+/Getty Images
Abel Mitja Varela / E + / Getty Images
  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.layout.GridPane;
  4. import javafx.stage.Stage;
  5. import javafx.geometry.Insets;
  6. import javafx.scene.text.Text;
  7. import javafx.scene.text.Font;
  8. import javafx.scene.text.FontWeight;
  9. import javafx.geometry.HPos;
  10. public class GridPaneExample extends Application {
  11. @Override
  12. public void start(Stage primaryStage) {
  13. //Create a GridPane for the Text Controls
  14. GridPane playerGrid = new GridPane();
  15. playerGrid.setHgap(10);
  16. playerGrid.setVgap(10);
  17. playerGrid.setPadding(new Insets(0, 10, 0, 10));
  18. //The title for the table spans for columns of the GridPane
  19. Text title = new Text("Top Scorers in English Premier League");
  20. title.setFont(Font.font("Arial", FontWeight.BOLD, 20));
  21. playerGrid.add(title, 0,0,4,1);
  22. //Each set of three Text controls makes up a row of the table
  23. Text rankTitle = new Text("Rank");
  24. rankTitle.setFont(Font.font("Arial", FontWeight.BOLD, 20));
  25. playerGrid.add(rankTitle, 0,3);
  26. Text playerTitle = new Text("Player");
  27. playerTitle.setFont(Font.font("Arial", FontWeight.BOLD, 20));
  28. playerGrid.add(playerTitle, 1,3);
  29. Text goalTitle = new Text("Goals");
  30. goalTitle.setFont(Font.font("Arial", FontWeight.BOLD, 20));
  31. playerGrid.add(goalTitle, 2,3);
  32. Text rank1 = new Text("1");
  33. rank1.setFont(Font.font("Arial", 16));
  34. GridPane.setHalignment(rank1, HPos.CENTER);
  35. playerGrid.add(rank1, 0,4);
  36. Text player1 = new Text("Alan Shearer");
  37. player1.setFont(Font.font("Arial", 16));
  38. playerGrid.add(player1, 1,4);
  39. Text goals1 = new Text("260");
  40. goals1.setFont(Font.font("Arial", 16));
  41. GridPane.setHalignment(goals1, HPos.CENTER);
  42. playerGrid.add(goals1, 2,4);
  43. Text rank2 = new Text("2");
  44. rank2.setFont(Font.font("Arial", 16));
  45. GridPane.setHalignment(rank2, HPos.CENTER);
  46. playerGrid.add(rank2, 0,5);
  47. Text player2 = new Text("Andrew Cole");
  48. player2.setFont(Font.font("Arial", 16));
  49. playerGrid.add(player2, 1,5);
  50. Text goals2 = new Text("187");
  51. goals2.setFont(Font.font("Arial", 16));
  52. GridPane.setHalignment(goals2, HPos.CENTER);
  53. playerGrid.add(goals2, 2,5);
  54. Text rank3 = new Text("3");
  55. rank3.setFont(Font.font("Arial", 16));
  56. GridPane.setHalignment(rank3, HPos.CENTER);
  57. playerGrid.add(rank3, 0,6);
  58. Text player3 = new Text("Thierry Henry");
  59. player3.setFont(Font.font("Arial", 16));
  60. playerGrid.add(player3, 1,6);
  61. Text goals3 = new Text("175");
  62. goals3.setFont(Font.font("Arial", 16));
  63. GridPane.setHalignment(goals3, HPos.CENTER);
  64. playerGrid.add(goals3, 2,6);
  65. Text rank4 = new Text("4");
  66. rank4.setFont(Font.font("Arial", 16));
  67. GridPane.setHalignment(rank4, HPos.CENTER);
  68. playerGrid.add(rank4, 0,7);
  69. Text player4 = new Text("Frank Lampard");
  70. player4.setFont(Font.font("Arial", 16));
  71. playerGrid.add(player4, 1,7);
  72. Text goals4 = new Text("165");
  73. goals4.setFont(Font.font("Arial", 16));
  74. GridPane.setHalignment(goals4, HPos.CENTER);
  75. playerGrid.add(goals4, 2,7);
  76. Text rank5 = new Text("5");
  77. rank5.setFont(Font.font("Arial", 16));
  78. GridPane.setHalignment(rank5, HPos.CENTER);
  79. playerGrid.add(rank5, 0,8);
  80. Text player5 = new Text("Robbie Fowler");
  81. player5.setFont(Font.font("Arial", 16));
  82. playerGrid.add(player5, 1,8);
  83. Text goals5 = new Text("162");
  84. goals5.setFont(Font.font("Arial", 16));
  85. GridPane.setHalignment(goals5, HPos.CENTER);
  86. playerGrid.add(goals5, 2,8);
  87. //playerGrid.setGridLinesVisible(true);
  88. Scene scene = new Scene(playerGrid, 500, 500);
  89. primaryStage.setTitle("Goal Scorers!");
  90. primaryStage.setScene(scene);
  91. primaryStage.show();
  92. }
  93. /**
  94. * The main() method is ignored in correctly deployed JavaFX application.
  95. * main() serves only as fallback in case the application can not be
  96. * launched through deployment artifacts, e.g., in IDEs with limited FX
  97. * support. NetBeans ignores main().
  98. *
  99. * @param args the command line arguments
  100. */
  101. public static void main(String[] args) {
  102. launch(args);
  103. }
  104. }

翻译自: https://www.thoughtco.com/gridpane-example-program-2033950

小程序示例官方源代码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/232764
推荐阅读