Javafx-layout-panes-hbox
提供:Dev Guides
JavaFX-レイアウトペインHBox
アプリケーションのレイアウトでHBoxを使用する場合、すべてのノードは単一の水平行に設定されます。
パッケージ javafx.scene.layout の HBox という名前のクラスは、HBoxペインを表します。 このクラスには5つのプロパティが含まれています-
- alignment -このプロパティは、HBoxの境界内のノードの配置を表します。 セッターメソッド* setAlignment()*を使用して、このプロパティに値を設定できます。
- fillHeight -このプロパティはブール型であり、これをtrueに設定すると、HBoxのサイズ変更可能なノードはHBoxの高さに合わせてサイズ変更されます。 セッターメソッド* setFillHeight()*を使用して、このプロパティに値を設定できます。
- spacing -このプロパティはdouble型で、HBoxの子間のスペースを表します。 セッターメソッド* setSpacing()*を使用して、このプロパティに値を設定できます。
これらに加えて、このクラスはまた、いくつかのメソッドを提供します-
- * setHgrow()*-HBoxに含まれる場合の子の水平方向の成長優先度を設定します。 このメソッドは、ノードと優先度の値を受け入れます。
- * setMargin()*-このメソッドを使用すると、HBoxにマージンを設定できます。 このメソッドは、ノードとInsetsクラスのオブジェクト(長方形領域の4辺の内部オフセットのセット)を受け入れます。
例
次のプログラムは、HBoxレイアウトの例です。 ここでは、テキストフィールドと2つのボタン、playとstopを挿入しています。 これは、10の間隔で行われ、それぞれに寸法のマージンがあります(10、10、10、10)。
このコードを HBoxExample.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.HBox;
public class HBoxExample 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 HBox class
HBox hbox = new HBox();
//Setting the space between the nodes of a HBox pane
hbox.setSpacing(10);
//Setting the margin to the nodes
hbox.setMargin(textField, new Insets(20, 20, 20, 20));
hbox.setMargin(playButton, new Insets(20, 20, 20, 20));
hbox.setMargin(stopButton, new Insets(20, 20, 20, 20));
//retrieving the observable list of the HBox
ObservableList list = hbox.getChildren();
//Adding all the nodes to the observable list (HBox)
list.addAll(textField, playButton, stopButton);
//Creating a scene object
Scene scene = new Scene(hbox);
//Setting title to the Stage
stage.setTitle("Hbox 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 HBoxExample.java
java HBoxExample.java
実行時に、上記のプログラムは以下に示すようにJavaFXウィンドウを生成します。