`

JavaFX学习之Layout

阅读更多
   Region 继承parent,被定义为屏幕的一块,可以包含其它node,并且可以用css修饰。region只有继承parent的getChildren()方法,是保护类型,所以在外面不能添加子节点,所以一般都是重写region。从region的定义中看到,region并没有坐布局处理,仅仅是包含其它node,所以子节点需要自己去控制布局。一般都是重写parent的layoutChildren去布局子节点,不过region提供了单个节点在region上布局的方法。
region的一些布局方法:
layoutInArea(Node child, double areaX, double areaY, double areaWidth, double areaHeight, double areaBaselineOffset, HPos halignment, VPos valignment)
layoutInArea(Node child, double areaX, double areaY, double areaWidth, double areaHeight, double areaBaselineOffset, Insets margin, boolean fillWidth, boolean fillHeight, HPos halignment, VPos valignment) 
layoutInArea(Node child, double areaX, double areaY, double areaWidth, double areaHeight, double areaBaselineOffset, Insets margin, HPos halignment, VPos valignment) 
positionInArea(Node child, double areaX, double areaY, double areaWidth, double areaHeight, double areaBaselineOffset, HPos halignment, VPos valignment) 
positionInArea(Node child, double areaX, double areaY, double areaWidth, double areaHeight, double areaBaselineOffset, Insets margin, HPos halignment, VPos valignment) 


其它一些方法:
setSnapToPixel(boolean value) 
snapPosition(double value) 
snapSize(double value) 
snapSpace(double value) 
resize(double width, double height) 
setPadding(Insets value) 


属性:
height,width ,prefHeight ,prefWidth ,minHeight ,minWidth ,maxHeight ,maxWidth 设置宽高
snapToPixel
padding 

   Pane 继承Region,所有布局类的父类。被定为子类已经被控制好布局,用户只需要添删。所以当你的父类是布局类时,layoutX,Y设置可能无效,因为被父类管理了。Pane可以直接使用,它并没有设置子节点的布局。
  
        Pane canvas = new Pane();
		canvas.setStyle("-fx-background-color: black;");
		canvas.setPrefSize(200, 200);
		Circle circle = new Circle(50, Color.BLUE);
		circle.setTranslateX(50);
		circle.setTranslateY(50);
		Rectangle rectangle = new Rectangle(100, 100, Color.RED);
		rectangle.relocate(70, 70);
		canvas.getChildren().addAll(circle, rectangle);
   

可以正常的设置layoutX,Y,也可以设置相对父类的位置。

   HBox and VBox   水平和垂直盒子布局,也就是从左到右布局和从上到下布局。其子节点的布局已经被父类安排好,自己设置无效,不过可以设置translate。
	public void init(HBox root){
		Button b1 = new Button("open");
		b1.setTranslateY(100);
		Button b2 = new Button("close");
		TableView tableView = new TableView();
		HBox.setHgrow(tableView, Priority.ALWAYS);
		root.setSpacing(20);
		root.setStyle("-fx-background-color:#EEEEEE");
		root.getChildren().addAll(b1,b2,tableView);
	}

HBox是水平布局,所以一般子节点只设置宽度,而子节点高度一般都由父类管理自动扩展填充,若不想由父类控制,则设置setFillHeight(boolean value)为false,取消父类的自动填充。

方法:
setFillHeight(boolean value) //设置是否自动填充高度,为false则其子节点可以设置自己的高度
setSpacing(double value) //二个物体间隔
setHgrow(Node child, Priority value) //因为HBox是水平布局,子节点的宽度不会跟随父类一起扩展,当父类扩大时,水平方向则有空白,若想要其子节点随父类扩展而扩展则调用该方法。设置子节点水平方向动填充父节点,可以同时设置多个自己点自动扩充。
setMargin(Node child, Insets value) //为子节点间设置margin space
setAlignment(Pos value) //控制子节点的位置
layoutChildren() //布局的时候用,一般是继承该类的子类,重写这个方法
getContentBias() 

VBox和HBox一样,只是方向变了

  AnchorPane 锚布局,通过设置其与边界的偏移位置固定位置
 
        AnchorPane anchorpane = new AnchorPane();
	    anchorpane.setPrefSize(400, 400);
	    Button buttonSave = new Button("Save");
	    Button buttonCancel = new Button("Cancel");
	    Button buttonopen = new Button("open");
	    Button buttonoclose = new Button("Close");
	    anchorpane.getChildren().addAll(buttonSave,buttonCancel,buttonopen,buttonoclose);   // Add grid from Example 1-5
	    AnchorPane.setBottomAnchor(buttonSave, 8.0);
	    AnchorPane.setRightAnchor(buttonCancel, 5.0);
	    AnchorPane.setRightAnchor(buttonopen, 10.0);
	    AnchorPane.setBottomAnchor(buttonopen, 10.0);
	    AnchorPane.setLeftAnchor(buttonoclose, 5.0);
  

 
  BorderPane 边界布局,分为上下左右中5块。 除了设置位置外,没什么特别的方法。

  FlowPane 流布局,从左往右或从上往下,满了自动换行
方法:
setOrientation(Orientation value)  //设置流的方向,水平或垂直
setPrefWrapLength(double value) ,prefWrapLengthProperty()   //因为流布局当区域满了会自动换行,所以这里可以设置具体的换行要求(或换列)
setHgap(double value) ,setVgap(double value)  //子节点之间可以设置间隙,水平方向的和垂直方向的
setRowValignment(VPos value),setColumnHalignment(HPos value)   //row是当水平布局时设置,column是当垂直布局时设置。 均是设置其位置,若你节点的位置刚好等于你那一行位置时,那设置就感觉不到变化。可以弄一个大的node,一个小的node,此时设置就以看到小的node有明显位置变化。


  GridPane  网格布局,每个cell大小一样,子节点按位置存放。
位置设置的一些方法:
add(Node child, int columnIndex, int rowIndex, int colspan, int rowspan)  //添加一个节点,位于的列,行,占几列,占几行。索引从0开始

 
  StackPane 栈布局,子节点存放是一层层加,像栈一样,stackPane会resize子节点扩充stackPane,并且子节点自己设置大小无效,但是当子节点不能resize,或则限制了resize时,那么stackPane会把子节点放置中间。
StackPane stack = new StackPane();
Label go = new Label("Go!");
Label to = new Label("To!");
stack.setPrefSize(200,200);
HBox box = new HBox();
box.setPrefSize(50, 50);
box.setStyle("-fx-background-color:#EEEEEE");
System.out.println(box.isResizable());
stack.getChildren().addAll(box, go,to);

可以看到box被resize了,尽管它自己设置了大小。

方法:
clearConstraints(Node child)

  TilePane 瓦片布局,有点向flowpane,但是它的每一个节点也就是每一块瓦片都一样大小
方法:
setPrefTileWidth(double value) ,setPrefTileHeight(double value)  设置单元瓦片的大小


  RowConstraints 行约束
  ColumnConstraints 列约束
  ConstraintsBase 基本约束
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics