JavaFX FlowPane布局

1- FlowPane布局

FlowPane是一个容器。它在一行上排列连续的子组件,并且如果当前行填满了以后,则自动将子组件向下推到下一行。

2- FlowPane示例

  1. import javafx.application.Application;
  2. import javafx.geometry.Insets;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.Button;
  5. import javafx.scene.control.CheckBox;
  6. import javafx.scene.control.RadioButton;
  7. import javafx.scene.control.TextField;
  8. import javafx.scene.layout.FlowPane;
  9. import javafx.stage.Stage;
  10. public class Main extends Application {
  11. @Override
  12. public void start(Stage primaryStage) throws Exception {
  13. FlowPane root = new FlowPane();
  14. root.setHgap(10);
  15. root.setVgap(20);
  16. root.setPadding(new Insets(15,15,15,15));
  17. // Button 1
  18. Button button1= new Button("Button1");
  19. root.getChildren().add(button1);
  20. // Button 2
  21. Button button2 = new Button("Button2");
  22. button2.setPrefSize(100, 100);
  23. root.getChildren().add(button2);
  24. // TextField
  25. TextField textField = new TextField("Text Field");
  26. textField.setPrefWidth(110);
  27. root.getChildren().add(textField);
  28. // CheckBox
  29. CheckBox checkBox = new CheckBox("Check Box");
  30. root.getChildren().add(checkBox);
  31. // RadioButton
  32. RadioButton radioButton = new RadioButton("Radio Button");
  33. root.getChildren().add(radioButton);
  34. Scene scene = new Scene(root, 550, 250);
  35. primaryStage.setTitle("FlowPane Layout Demo");
  36. primaryStage.setScene(scene);
  37. primaryStage.show();
  38. }
  39. public static void main(String[] args) {
  40. launch(args);
  41. }
  42. }

image.png