JavaFX 各种内建built-in布局管理器的使用(转载)

[java]  view plain  copy
  1. package com.han;  
  2.   
  3. import javafx.application.Application;  
  4. import javafx.geometry.Insets;  
  5. import javafx.geometry.Pos;  
  6. import javafx.geometry.VPos;  
  7. import javafx.scene.Scene;  
  8. import javafx.scene.control.Button;  
  9. import javafx.scene.control.Hyperlink;  
  10. import javafx.scene.image.ImageView;  
  11. import javafx.scene.layout.AnchorPane;  
  12. import javafx.scene.layout.BorderPane;  
  13. import javafx.scene.layout.FlowPane;  
  14. import javafx.scene.layout.GridPane;  
  15. import javafx.scene.layout.HBox;  
  16. import javafx.scene.layout.Priority;  
  17. import javafx.scene.layout.StackPane;  
  18. import javafx.scene.layout.VBox;  
  19. import javafx.scene.paint.Color;  
  20. import javafx.scene.paint.CycleMethod;  
  21. import javafx.scene.paint.LinearGradient;  
  22. import javafx.scene.paint.Stop;  
  23. import javafx.scene.shape.Rectangle;  
  24. import javafx.scene.text.Font;  
  25. import javafx.scene.text.FontWeight;  
  26. import javafx.scene.text.Text;  
  27. import javafx.stage.Stage;  
  28.   
  29. public class Layouts extends Application {  
  30.   
  31.     public static void main(String[] args) {  
  32.         launch(args);  
  33.     }  
  34.   
  35.     @Override  
  36.     public void start(Stage primaryStage) throws Exception {  
  37.         // 初始化  
  38.         primaryStage.setTitle("Layouts in JavaFX");  
  39.         BorderPane root = new BorderPane();  
  40.         Scene scene = new Scene(root, 700500);  
  41.         scene.getStylesheets().add(  
  42.                 getClass().getResource("style.css").toString());  
  43.         primaryStage.setScene(scene);  
  44.   
  45.         // 定义顶部的2个button  
  46.         Button button1 = new Button("Current");  
  47.         button1.setStyle("-fx-base: red;");  
  48.         Button button2 = new Button("Projected");  
  49.         button1.setPrefWidth(100);  
  50.         button2.setPrefWidth(100);  
  51.         button1.setPrefHeight(20);  
  52.         button2.setPrefHeight(20);  
  53.   
  54.         // 定义帮助按钮  
  55.         StackPane stackPane = new StackPane();  
  56.         Rectangle helpIcon = new Rectangle();  
  57.         helpIcon.setWidth(30.0);  
  58.         helpIcon.setHeight(25.0);  
  59.         helpIcon.setArcWidth(3.5);  
  60.         helpIcon.setArcHeight(3.5);  
  61.         helpIcon.setFill(new LinearGradient(0001true,  
  62.                 CycleMethod.NO_CYCLE, new Stop(0, Color.web("#4977A3")),  
  63.                 new Stop(0.5, Color.web("#B0C6DA")), new Stop(1, Color  
  64.                         .web("#9CB6CF"))));  
  65.         helpIcon.setStroke(Color.web("#D0E6FA"));  
  66.         Text text = new Text();  
  67.         text.setText("?");  
  68.         text.setFont(Font.font("Verdana", FontWeight.BOLD, 18));  
  69.         text.setFill(Color.WHITE);  
  70.         text.setStroke(Color.web("#7080A0"));  
  71.         stackPane.getChildren().addAll(helpIcon, text);  
  72.         stackPane.setAlignment(Pos.CENTER_RIGHT);// 所有节点向右对齐  
  73.         StackPane.setMargin(text, new Insets(01000));// Center "?"  
  74.   
  75.         // 使用HBox来设置BorderPane的顶部位置  
  76.         HBox hBox = new HBox();  
  77.         hBox.getChildren().addAll(button1, button2, stackPane);  
  78.         // give the stack pane any extra space  
  79.         HBox.setHgrow(stackPane, Priority.ALWAYS);  
  80.         hBox.getStyleClass().add("h-box");  
  81.         root.setTop(hBox);  
  82.   
  83.         // 使用VBox来设置BorderPane的左边位置  
  84.         VBox vBox = new VBox();  
  85.         vBox.getStyleClass().add("v-box");  
  86.         Text title = new Text("Data");  
  87.         title.setFont(Font.font("Arial", FontWeight.BOLD, 14));  
  88.         vBox.getChildren().add(title);  
  89.         Hyperlink[] options = new Hyperlink[] { new Hyperlink("Sales"),  
  90.                 new Hyperlink("Marketing"), new Hyperlink("Distribution"),  
  91.                 new Hyperlink("Costs") };  
  92.         for (Hyperlink e : options) {  
  93.             VBox.setMargin(e, new Insets(0008));  
  94.             vBox.getChildren().add(e);  
  95.         }  
  96.         root.setLeft(vBox);  
  97.   
  98.         // 使用AnchorPane来设置BorderPane的中间位置  
  99.         // AnchorPane中包含GridPane  
  100.         GridPane gridPane = new GridPane();  
  101.         gridPane.getStyleClass().add("grid-pane");  
  102.         gridPane.setGridLinesVisible(true);  
  103.         // Category in column 2, row 1  
  104.         Text category = new Text("Sales:");  
  105.         category.setFont(Font.font("Arial", FontWeight.BOLD, 20));  
  106.         gridPane.add(category, 10);  
  107.         // Title in column 3, row 1  
  108.         Text chartTitle = new Text("Current year");  
  109.         chartTitle.setFont(Font.font("Arial", FontWeight.BOLD, 20));  
  110.         gridPane.add(chartTitle, 20);  
  111.         // Subtitle in column 2-3, row 2  
  112.         Text chartSubtitle = new Text("Goods and Services");  
  113.         gridPane.add(chartSubtitle, 1121);  
  114.         // House icon in column 1, row 1-2  
  115.         ImageView houseIcon = new ImageView(getClass().getResource(  
  116.                 "Graphics/house.png").toString());  
  117.         gridPane.add(houseIcon, 0012);  
  118.         // Left label in column 1 (bottom), row 3  
  119.         Text goodsPercent = new Text("Goods\n80%");  
  120.         GridPane.setValignment(goodsPercent, VPos.BOTTOM);  
  121.         gridPane.add(goodsPercent, 02);  
  122.         // Chart in column 2-3, row 3  
  123.         ImageView chartImage = new ImageView(getClass().getResource(  
  124.                 "Graphics/piechart.png").toExternalForm());  
  125.         gridPane.add(chartImage, 1221);  
  126.         // Right label in column 4 (top), row 3  
  127.         Text servicesPercent = new Text("Services\n20%");  
  128.         GridPane.setValignment(servicesPercent, VPos.TOP);  
  129.         gridPane.add(servicesPercent, 32);  
  130.   
  131.         Button buttonSave = new Button("Save");  
  132.         Button buttonCancel = new Button("Cancel");  
  133.         HBox hBox2 = new HBox();  
  134.         hBox2.setSpacing(10);  
  135.         hBox2.getChildren().addAll(buttonSave, buttonCancel);  
  136.         AnchorPane anchorPane = new AnchorPane();  
  137.         anchorPane.getChildren().addAll(gridPane, hBox2);  
  138.         AnchorPane.setBottomAnchor(hBox2, 10.0);  
  139.         AnchorPane.setRightAnchor(hBox2, 10.0);  
  140.         AnchorPane.setTopAnchor(gridPane, 10.0);  
  141.         // Add the anchorPane to the center of border pane  
  142.         root.setCenter(anchorPane);  
  143.   
  144.         // 使用FlowPane来设置BorderPane的右边位置  
  145.         // (注:TilePane和FlowPane类似,只不过与FlowPane不同,TilePane每个tile的大小是相同的)  
  146.         FlowPane flowPane = new FlowPane();// Define a horizontal flow pane  
  147.         flowPane.getStyleClass().add("flow-pane");  
  148.         for (int i = 0; i < 8; i++) {  
  149.             flowPane.getChildren().add(  
  150.                     new ImageView(getClass().getResource(  
  151.                             "Graphics/chart_" + (i + 1) + ".png")  
  152.                             .toExternalForm()));  
  153.         }  
  154.         root.setRight(flowPane);  
  155.   
  156.         // 设置初始化焦点获得者(after the component has been realized, but just before the  
  157.         // frame is displayed)  
  158.         button2.requestFocus();  
  159.         // 显示程序界面  
  160.         primaryStage.show();  
  161.     }  
  162.   
  163. }  
[java]  view plain  copy
  1. package com.han;  
  2.   
  3. import javafx.application.Application;  
  4. import javafx.geometry.Insets;  
  5. import javafx.geometry.Pos;  
  6. import javafx.geometry.VPos;  
  7. import javafx.scene.Scene;  
  8. import javafx.scene.control.Button;  
  9. import javafx.scene.control.Hyperlink;  
  10. import javafx.scene.image.ImageView;  
  11. import javafx.scene.layout.AnchorPane;  
  12. import javafx.scene.layout.BorderPane;  
  13. import javafx.scene.layout.FlowPane;  
  14. import javafx.scene.layout.GridPane;  
  15. import javafx.scene.layout.HBox;  
  16. import javafx.scene.layout.Priority;  
  17. import javafx.scene.layout.StackPane;  
  18. import javafx.scene.layout.VBox;  
  19. import javafx.scene.paint.Color;  
  20. import javafx.scene.paint.CycleMethod;  
  21. import javafx.scene.paint.LinearGradient;  
  22. import javafx.scene.paint.Stop;  
  23. import javafx.scene.shape.Rectangle;  
  24. import javafx.scene.text.Font;  
  25. import javafx.scene.text.FontWeight;  
  26. import javafx.scene.text.Text;  
  27. import javafx.stage.Stage;  
  28.   
  29. public class Layouts extends Application {  
  30.   
  31.     public static void main(String[] args) {  
  32.         launch(args);  
  33.     }  
  34.   
  35.     @Override  
  36.     public void start(Stage primaryStage) throws Exception {  
  37.         // 初始化  
  38.         primaryStage.setTitle("Layouts in JavaFX");  
  39.         BorderPane root = new BorderPane();  
  40.         Scene scene = new Scene(root, 700500);  
  41.         scene.getStylesheets().add(  
  42.                 getClass().getResource("style.css").toString());  
  43.         primaryStage.setScene(scene);  
  44.   
  45.         // 定义顶部的2个button  
  46.         Button button1 = new Button("Current");  
  47.         button1.setStyle("-fx-base: red;");  
  48.         Button button2 = new Button("Projected");  
  49.         button1.setPrefWidth(100);  
  50.         button2.setPrefWidth(100);  
  51.         button1.setPrefHeight(20);  
  52.         button2.setPrefHeight(20);  
  53.   
  54.         // 定义帮助按钮  
  55.         StackPane stackPane = new StackPane();  
  56.         Rectangle helpIcon = new Rectangle();  
  57.         helpIcon.setWidth(30.0);  
  58.         helpIcon.setHeight(25.0);  
  59.         helpIcon.setArcWidth(3.5);  
  60.         helpIcon.setArcHeight(3.5);  
  61.         helpIcon.setFill(new LinearGradient(0001true,  
  62.                 CycleMethod.NO_CYCLE, new Stop(0, Color.web("#4977A3")),  
  63.                 new Stop(0.5, Color.web("#B0C6DA")), new Stop(1, Color  
  64.                         .web("#9CB6CF"))));  
  65.         helpIcon.setStroke(Color.web("#D0E6FA"));  
  66.         Text text = new Text();  
  67.         text.setText("?");  
  68.         text.setFont(Font.font("Verdana", FontWeight.BOLD, 18));  
  69.         text.setFill(Color.WHITE);  
  70.         text.setStroke(Color.web("#7080A0"));  
  71.         stackPane.getChildren().addAll(helpIcon, text);  
  72.         stackPane.setAlignment(Pos.CENTER_RIGHT);// 所有节点向右对齐  
  73.         StackPane.setMargin(text, new Insets(01000));// Center "?"  
  74.   
  75.         // 使用HBox来设置BorderPane的顶部位置  
  76.         HBox hBox = new HBox();  
  77.         hBox.getChildren().addAll(button1, button2, stackPane);  
  78.         // give the stack pane any extra space  
  79.         HBox.setHgrow(stackPane, Priority.ALWAYS);  
  80.         hBox.getStyleClass().add("h-box");  
  81.         root.setTop(hBox);  
  82.   
  83.         // 使用VBox来设置BorderPane的左边位置  
  84.         VBox vBox = new VBox();  
  85.         vBox.getStyleClass().add("v-box");  
  86.         Text title = new Text("Data");  
  87.         title.setFont(Font.font("Arial", FontWeight.BOLD, 14));  
  88.         vBox.getChildren().add(title);  
  89.         Hyperlink[] options = new Hyperlink[] { new Hyperlink("Sales"),  
  90.                 new Hyperlink("Marketing"), new Hyperlink("Distribution"),  
  91.                 new Hyperlink("Costs") };  
  92.         for (Hyperlink e : options) {  
  93.             VBox.setMargin(e, new Insets(0008));  
  94.             vBox.getChildren().add(e);  
  95.         }  
  96.         root.setLeft(vBox);  
  97.   
  98.         // 使用AnchorPane来设置BorderPane的中间位置  
  99.         // AnchorPane中包含GridPane  
  100.         GridPane gridPane = new GridPane();  
  101.         gridPane.getStyleClass().add("grid-pane");  
  102.         gridPane.setGridLinesVisible(true);  
  103.         // Category in column 2, row 1  
  104.         Text category = new Text("Sales:");  
  105.         category.setFont(Font.font("Arial", FontWeight.BOLD, 20));  
  106.         gridPane.add(category, 10);  
  107.         // Title in column 3, row 1  
  108.         Text chartTitle = new Text("Current year");  
  109.         chartTitle.setFont(Font.font("Arial", FontWeight.BOLD, 20));  
  110.         gridPane.add(chartTitle, 20);  
  111.         // Subtitle in column 2-3, row 2  
  112.         Text chartSubtitle = new Text("Goods and Services");  
  113.         gridPane.add(chartSubtitle, 1121);  
  114.         // House icon in column 1, row 1-2  
  115.         ImageView houseIcon = new ImageView(getClass().getResource(  
  116.                 "Graphics/house.png").toString());  
  117.         gridPane.add(houseIcon, 0012);  
  118.         // Left label in column 1 (bottom), row 3  
  119.         Text goodsPercent = new Text("Goods\n80%");  
  120.         GridPane.setValignment(goodsPercent, VPos.BOTTOM);  
  121.         gridPane.add(goodsPercent, 02);  
  122.         // Chart in column 2-3, row 3  
  123.         ImageView chartImage = new ImageView(getClass().getResource(  
  124.                 "Graphics/piechart.png").toExternalForm());  
  125.         gridPane.add(chartImage, 1221);  
  126.         // Right label in column 4 (top), row 3  
  127.         Text servicesPercent = new Text("Services\n20%");  
  128.         GridPane.setValignment(servicesPercent, VPos.TOP);  
  129.         gridPane.add(servicesPercent, 32);  
  130.   
  131.         Button buttonSave = new Button("Save");  
  132.         Button buttonCancel = new Button("Cancel");  
  133.         HBox hBox2 = new HBox();  
  134.         hBox2.setSpacing(10);  
  135.         hBox2.getChildren().addAll(buttonSave, buttonCancel);  
  136.         AnchorPane anchorPane = new AnchorPane();  
  137.         anchorPane.getChildren().addAll(gridPane, hBox2);  
  138.         AnchorPane.setBottomAnchor(hBox2, 10.0);  
  139.         AnchorPane.setRightAnchor(hBox2, 10.0);  
  140.         AnchorPane.setTopAnchor(gridPane, 10.0);  
  141.         // Add the anchorPane to the center of border pane  
  142.         root.setCenter(anchorPane);  
  143.   
  144.         // 使用FlowPane来设置BorderPane的右边位置  
  145.         // (注:TilePane和FlowPane类似,只不过与FlowPane不同,TilePane每个tile的大小是相同的)  
  146.         FlowPane flowPane = new FlowPane();// Define a horizontal flow pane  
  147.         flowPane.getStyleClass().add("flow-pane");  
  148.         for (int i = 0; i < 8; i++) {  
  149.             flowPane.getChildren().add(  
  150.                     new ImageView(getClass().getResource(  
  151.                             "Graphics/chart_" + (i + 1) + ".png")  
  152.                             .toExternalForm()));  
  153.         }  
  154.         root.setRight(flowPane);  
  155.   
  156.         // 设置初始化焦点获得者(after the component has been realized, but just before the  
  157.         // frame is displayed)  
  158.         button2.requestFocus();  
  159.         // 显示程序界面  
  160.         primaryStage.show();  
  161.     }  
  162.   
  163. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值