Javafx-layout-panes-vbox
提供:Dev Guides
JavaFX-レイアウトペインVBox
アプリケーションのレイアウトとしてVBoxを使用する場合、すべてのノードは単一の垂直列に設定されます。
パッケージ javafx.scene.layout の VBox という名前のクラスは、VBoxペインを表します。 このクラスには5つのプロパティが含まれています-
- alignment -このプロパティは、VBoxの境界内のノードの配置を表します。 セッターメソッド* setAlignment()*を使用して、このプロパティに値を設定できます。
- fillHeight -このプロパティはブール型であり、これをtrueに設定すると、 VBoxのサイズ変更可能なノードは、VBoxの高さに合わせてサイズ変更されます。 セッターメソッド* setFillHeight()*を使用して、このプロパティに値を設定できます。
- spacing -このプロパティはdouble型で、VBoxの子間のスペースを表します。 セッターメソッド* setSpacing()*を使用して、このプロパティに値を設定できます。
これらに加えて、このクラスは次のメソッドも提供します-
- * setVgrow()*-VBoxに含まれる場合の子の垂直成長優先度を設定します。 このメソッドは、ノードと優先度の値を受け入れます。
- * setMargin()*-このメソッドを使用して、VBoxにマージンを設定できます。 このメソッドは、ノードとInsetsクラスのオブジェクト(長方形領域の4辺の内部オフセットのセット)を受け入れます
例
次のプログラムは、 VBox レイアウトの例です。 ここでは、テキストフィールドと2つのボタン、playとstopを挿入しています。 これは、10の間隔で行われ、それぞれに寸法のマージンがあります(10、10、10、10)。
このコードを VBoxExample.java という名前のファイルに保存します。
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.scene.layout.VBox;
public class VBoxExample extends Application {
@Override
public void start(Stage stage) {
//creating a text field
TextField textField = new TextField();
//Creating the play button
Button playButton = new Button("Play");
//Creating the stop button
Button stopButton = new Button("stop");
//Instantiating the VBox class
VBox vBox = new VBox();
//Setting the space between the nodes of a VBox pane
vBox.setSpacing(10);
//Setting the margin to the nodes
vBox.setMargin(textField, new Insets(20, 20, 20, 20));
vBox.setMargin(playButton, new Insets(20, 20, 20, 20));
vBox.setMargin(stopButton, new Insets(20, 20, 20, 20));
//retrieving the observable list of the VBox
ObservableList list = vBox.getChildren();
//Adding all the nodes to the observable list
list.addAll(textField, playButton, stopButton);
//Creating a scene object
Scene scene = new Scene(vBox);
//Setting title to the Stage
stage.setTitle("Vbox Example");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します。
javac VBoxExample.java
java VBoxExample.java
実行時に、上記のプログラムは以下に示すようにJavaFXウィンドウを生成します。