FlowPane将结点按照加入的次序,水平方向从左到右水平或者垂直方向从上到下地放置。
package Chap14; import Chap12.Test; import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.FlowPane; import javafx.stage.Stage; public class ShowFlowPane extends Application { @Override public void start(Stage primaryStage) throws Exception { FlowPane pane = new FlowPane(); //创建一个Insets并给出边框大小(顶、右、底、左) pane.setPadding(new Insets(11,12,13,14)); //水平间距 pane.setHgap(5); //垂直间距 pane.setVgap(5); //Place nodes in the pane pane.getChildren().addAll(new Label("First Name:"),new TextField(),new Label("MI:")); //显式引用,因为要来设置属性 TextField tfmi = new TextField(); //将MI文本域的首选列数设置为1 tfmi.setPrefColumnCount(1); pane.getChildren().addAll(tfmi,new Label("Last Name:"),new TextField()); //Creat a scene and place it in the stage Scene scene = new Scene(pane,200,250); primaryStage.setTitle("ShowFlowPane"); primaryStage.setScene(scene); primaryStage.show(); } }
运行结果:
GridPane将结点安排在一个网格(矩阵)结构中。结点放在一个指定下标的列和行中。
package Chap14; import javafx.application.Application; import javafx.geometry.HPos; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.stage.Stage; public class ShowGridPane extends Application { @Override public void start(Stage primaryStage) throws Exception { GridPane pane = new GridPane(); //对齐方式设为居中 pane.setAlignment(Pos.CENTER); pane.setPadding(new Insets(11.5,12.5,13.5,14.5)); pane.setHgap(5.5); pane.setVgap(5.5); //将标签置于0行0列 pane.add(new Label("First Name:"),0,0); pane.add(new TextField(),1,0); pane.add(new Label("MI:"),0,1); pane.add(new TextField(),1,1); pane.add(new Label("Last Name:"),0,2); pane.add(new TextField(),1,2); Button btAdd = new Button("Add Name"); pane.add(btAdd,1,3); //将按钮在单元格中右对齐 GridPane.setHalignment(btAdd, HPos.RIGHT); //创建场景并放在舞台 Scene scene = new Scene(pane); primaryStage.setTitle("ShowGridPane"); primaryStage.setScene(scene); primaryStage.show();//Display the stage } }
运行结果: