AnchorPane:绝对布局
@Override public void start(Stage primaryStage) throws Exception{ TextArea textArea = new TextArea(); AnchorPane ap = new AnchorPane(); AnchorPane.setTopAnchor(textArea,10.0); AnchorPane.setBottomAnchor(textArea,10.0); AnchorPane.setLeftAnchor(textArea,10.0); AnchorPane.setRightAnchor(textArea,10.0); //这样设置的话,textArea会居中显示,并随着ap的放大而放大。 ap.getChildren().add(textArea); Scene scene = new Scene(ap); primaryStage.setScene(scene); primaryStage.setTitle("标题栏"); primaryStage.setWidth(400); primaryStage.setHeight(400); primaryStage.show();//启动 }
HBox及VBox:线性布局(常用于菜单栏)
@Override public void start(Stage primaryStage) throws Exception{ Button btn = new Button("Button1"); Button btn2 = new Button("Button2"); Button btn3 = new Button("Button3"); AnchorPane ap = new AnchorPane(); HBox box = new HBox();//横向线性布局 hbox.setAlignment(Pos.CENTER)//设置子控件居中 box.setPrefHeight(400); box.setPrefWidth(400); box.setSpacing(10);//设置子控件的间距 box.getChildren().addAll(btn,btn2,btn3); ap.getChildren().add(box); VBox vbox = new VBox();//纵向线性布局 Scene scene = new Scene(ap); primaryStage.setScene(scene); primaryStage.setTitle("标题栏"); primaryStage.setWidth(400); primaryStage.setHeight(400); primaryStage.show();//启动 }
