FlowPane根據(jù)可用的水平間距布置一行中的節(jié)點當(dāng)水平空間小于所有節(jié)點“寬度"的總和時,將節(jié)點包裹到下一行。
默認(rèn)情況下,F(xiàn)lowPane布局從左到右流動子節(jié)點(Pos.TOP_LEFT)。
要更改流向?qū)R,請調(diào)用 setAlignment()
方法通過傳遞類型 Pos
的枚舉值。
以下代碼創(chuàng)建一個FlowPane布局,以從右到左(Pos.TOP_RIGHT)流動子節(jié)點。
FlowPane flowPane = new FlowPane(); flowPane.setAlignment(Pos.TOP_RIGHT); flowPane.getChildren().addAll(...); // child nodes to add.
向流窗格添加按鈕
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.FlowPane; import javafx.stage.Stage; /*from w w w .ja va2 s . c o m*/ public class Main extends Application { @Override public void start(Stage stage) { stage.setTitle("HTML"); stage.setWidth(500); stage.setHeight(500); Scene scene = new Scene(new Group()); FlowPane flow = new FlowPane(); flow.setVgap(8); flow.setHgap(4); flow.setPrefWrapLength(300); // preferred width = 300 for (int i = 0; i < 10; i++) { flow.getChildren().add(new Button("asdf")); } scene.setRoot(flow); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
上面的代碼生成以下結(jié)果。
FlowPane首選寬度允許兩列
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.FlowPane; import javafx.stage.Stage; //from w w w. j a v a 2 s. c om public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("FlowPane example"); FlowPane flowPane = new FlowPane(); flowPane.setPadding(new Insets(10, 10, 10, 10)); flowPane.setVgap(4); flowPane.setHgap(4); flowPane.setPrefWrapLength(210); Button btn = new Button(); for (int i = 0; i < 8; i++) { btn = new Button("Button"); btn.setPrefSize(100, 50); flowPane.getChildren().add(btn); } Scene scene = new Scene(flowPane); primaryStage.setScene(scene); primaryStage.show(); } }
上面的代碼生成以下結(jié)果。
更多建議: