如何使用 JavaFX 建立一個 VBox?
一旦為你的應用程式建立所有必須的節點,你就可以使用一個佈局來對它們進行整理。其中佈局是一個在給定的空間裡計算物件位置的過程。JavaFX 在 javafx.scene.layout 包中提供了各種佈局。
VBox
在 vbox 佈局中,節點在一個垂直列中進行整理。你可以透過例項化 javafx.scene.layout.VBox 類來在你的應用程式中建立一個 hbox。你可以使用 setPadding() 方法來設定 hbox 周圍的填充。
要向此面板新增節點,你可以將它們作為建構函式的引數傳遞,或者將它們新增到面板的可觀察列表中,如下所示 -
getChildren().addAll();
示例
import javafx.application.Application; import javafx.geometry.HPos; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.CheckBox; import javafx.scene.control.Label; import javafx.scene.control.Separator; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; import javafx.stage.Stage; public class VBoxExample extends Application { public void start(Stage stage) { //Creating the check boxes CheckBox checkBox1 = new CheckBox("Hindi"); CheckBox checkBox2 = new CheckBox("Gujarathi"); CheckBox checkBox3 = new CheckBox("Punjabi"); CheckBox checkBox4 = new CheckBox("Telugu"); CheckBox checkBox5 = new CheckBox("Tamil"); CheckBox checkBox6= new CheckBox("Malayalam"); //Creating a label Label label = new Label("Select known languages:"); Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12); label.setFont(font); //Creating a separator Separator sep = new Separator(); sep.setMaxWidth(80); sep.setHalignment(HPos.CENTER); //Adding the check boxes and separator to the pane VBox vBox = new VBox(5); vBox.setPadding(new Insets(5, 5, 5, 50)); vBox.getChildren().addAll(label, checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6); //Adding the separator after the 3rd check box vBox.getChildren().add(4, sep); //Setting the stage Scene scene = new Scene(vBox, 595, 200, Color.BEIGE); stage.setTitle("VBox Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
輸出
廣告