FlowPane
FlowPane
根据可用的可用水平或垂直空间布置行或列中的节点。当水平空间小于所有节点宽度的总和时,它将节点包装到下一行; 当垂直空间小于所有节点高度的总和时,它将节点包装到下一列。此示例说明了默认的水平布局:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
FlowPane root = new FlowPane();
for (int i=1; i<=15; i++) {
Button b1=new Button("Button "+String.valueOf(i));
root.getChildren().add(b1); //for adding button to root
}
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("FlowPane Layout");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
默认
FlowPane
构造函数:
FlowPane root = new FlowPane();
额外的
FlowPane
构造函数:
FlowPane() //Creates a horizontal FlowPane layout with hgap/vgap = 0 by default.
FlowPane(double hgap, double gap) //Creates a horizontal FlowPane layout with the specified hgap/vgap.
FlowPane(double hgap, double vgap, Node... children) //Creates a horizontal FlowPane layout with the specified hgap/vgap.
FlowPane(Node... children) //Creates a horizontal FlowPane layout with hgap/vgap = 0.
FlowPane(Orientation orientation) //Creates a FlowPane layout with the specified orientation and hgap/vgap = 0.
FlowPane(Orientation orientation, double hgap, double gap) //Creates a FlowPane layout with the specified orientation and hgap/vgap.
FlowPane(Orientation orientation, double hgap, double vgap, Node... children) //Creates a FlowPane layout with the specified orientation and hgap/vgap.
FlowPane(Orientation orientation, Node... children) //Creates a FlowPane layout with the specified orientation and hgap/vgap = 0.
向布局添加节点使用父
Pane
的add()
或addAll()
方法:
Button btn = new Button("Demo Button");
root.getChildren().add(btn);
root.getChildren().addAll(…);
默认情况下,FlowPane
从左到右布置子节点。要更改流对齐方式,请通过传入 Pos
类型的枚举值来调用 setAlignment()
方法。
一些常用的流动对齐:
root.setAlignment(Pos.TOP_RIGHT); //for top right
root.setAlignment(Pos.TOP_CENTER); //for top Center
root.setAlignment(Pos.CENTER); //for Center
root.setAlignment(Pos.BOTTOM_RIGHT); //for bottom right