JavaFX API具有將UI控件顯示到場(chǎng)景圖上的布局類(lèi)。
HBox布局類(lèi)將JavaFX子節(jié)點(diǎn)放在水平行中。新的子節(jié)點(diǎn)附加到右側(cè)的末尾。
默認(rèn)情況下,HBox布局尊重孩子的首選寬度和高度。
當(dāng)父節(jié)點(diǎn)不可調(diào)整大小時(shí),例如Group節(jié)點(diǎn),HBox的行高度被設(shè)置為子節(jié)點(diǎn)的最大優(yōu)選高度。
默認(rèn)情況下,每個(gè)子節(jié)點(diǎn)與左上(Pos.TOP_LEFT)位置對(duì)齊。
我們可以編程改變HBox的布局約束,例如邊框,填充,邊距,間距和對(duì)齊。
當(dāng)處理不可縮放的子節(jié)點(diǎn)(例如Shape節(jié)點(diǎn))時(shí),父節(jié)點(diǎn)考慮了Shape的矩形邊界(ParentInBounds),它的寬度和高度。
當(dāng)處理可調(diào)整大小的節(jié)點(diǎn)(如TextField控件)時(shí),父節(jié)點(diǎn)計(jì)算TextField水平增長(zhǎng)的可用空間。
要在HBox中水平生長(zhǎng)UI控件,請(qǐng)使用靜態(tài)HBox.setHgrow()方法。
以下代碼將TextField控件設(shè)置為在調(diào)整父HBox的寬度時(shí)水平增長(zhǎng):
TextField myTextField = new TextField(); HBox.setHgrow(myTextField, Priority.ALWAYS);
完整的源代碼。
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; import javafx.scene.paint.Color; import javafx.stage.Stage; /*from w w w. j a v a 2s . c o m*/ public class Main extends Application { @Override public void start(Stage primaryStage) { TextField myTextField = new TextField(); HBox hbox = new HBox(); hbox.getChildren().add(myTextField); HBox.setHgrow(myTextField, Priority.ALWAYS); Scene scene = new Scene(hbox, 320, 112, Color.rgb(0, 0, 0, 0)); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
上面的代碼生成以下結(jié)果。
下面的代碼為HBox添加了四個(gè)矩形,設(shè)置了HBox約束并展示了HBox布局控件的許多間距屬性。
矩形節(jié)點(diǎn)不可調(diào)整大小。
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.layout.HBox; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; // ww w. ja v a 2s .c om public class Main extends Application { @Override public void start(Stage primaryStage) { Group root = new Group(); Scene scene = new Scene(root, 300, 250); // 5 pixels space between child nodes HBox hbox = new HBox(5); // 1 pixel padding between child nodes only hbox.setPadding(new Insets(1)); Rectangle r1 = new Rectangle(10, 10); Rectangle r2 = new Rectangle(20, 100); Rectangle r3 = new Rectangle(50, 20); Rectangle r4 = new Rectangle(20, 50); HBox.setMargin(r1, new Insets(2, 2, 2, 2)); hbox.getChildren().addAll(r1, r2, r3, r4); root.getChildren().add(hbox); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
上面的代碼生成以下結(jié)果。
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; import javafx.scene.paint.Color; import javafx.stage.Stage; //from w w w . ja v a2 s .com public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle(""); Group root = new Group(); Scene scene = new Scene(root, 300, 250, Color.WHITE); HBox hbox = new HBox(); Button button1 = new Button("Add "); Button button2 = new Button("Remove "); HBox.setHgrow(button1, Priority.ALWAYS); HBox.setHgrow(button2, Priority.ALWAYS); button1.setMaxWidth(Double.MAX_VALUE); button2.setMaxWidth(Double.MAX_VALUE); hbox.getChildren().addAll(button1, button2); root.getChildren().add(hbox); primaryStage.setScene(scene); primaryStage.show(); } }
上面的代碼生成以下結(jié)果。
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; import javafx.scene.paint.Color; 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) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle(""); Group root = new Group(); Scene scene = new Scene(root, 300, 250, Color.WHITE); HBox hbox = new HBox(); Button button1 = new Button("Add "); Button button2 = new Button("Remove "); HBox.setHgrow(button1, Priority.ALWAYS); HBox.setHgrow(button2, Priority.ALWAYS); button1.setMaxWidth(Double.MAX_VALUE); button2.setMaxWidth(Double.MAX_VALUE); hbox.getChildren().addAll(button1, button2); hbox.setPrefWidth(400); root.getChildren().add(hbox); primaryStage.setScene(scene); primaryStage.show(); } }
上面的代碼生成以下結(jié)果。
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; import javafx.scene.paint.Color; import javafx.stage.Stage; /* ww w .j a va2 s . c om*/ public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle(""); Group root = new Group(); Scene scene = new Scene(root, 300, 250, Color.WHITE); HBox hbox = new HBox(8);//space Button button1 = new Button("Add "); Button button2 = new Button("Remove "); HBox.setHgrow(button1, Priority.ALWAYS); HBox.setHgrow(button2, Priority.ALWAYS); button1.setMaxWidth(Double.MAX_VALUE); button2.setMaxWidth(Double.MAX_VALUE); hbox.getChildren().addAll(button1, button2); hbox.setPrefWidth(400); root.getChildren().add(hbox); primaryStage.setScene(scene); primaryStage.show(); } }
上面的代碼生成以下結(jié)果。
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.stage.Stage; /*w ww.ja v a 2s. c o m*/ public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("HBox Test"); // HBox HBox hb = new HBox(); hb.setPadding(new Insets(15, 12, 15, 12)); hb.setSpacing(10); // Buttons Button btn1 = new Button(); btn1.setText("Button1"); hb.getChildren().add(btn1); Button btn2 = new Button(); btn2.setText("Button2"); hb.getChildren().add(btn2); Button btn3 = new Button(); btn3.setText("Button3"); hb.getChildren().add(btn3); Button btn4 = new Button(); btn4.setText("Button4"); hb.getChildren().add(btn4); // Adding HBox to the scene Scene scene = new Scene(hb); primaryStage.setScene(scene); primaryStage.show(); } }
上面的代碼生成以下結(jié)果。
更多建議: