Javafx-layout-gridpane
提供:Dev Guides
JavaFX-レイアウトGridPane
アプリケーションでグリッドペインを使用する場合、それに追加されるすべてのノードは、行と列のグリッドを形成するように配置されます。 このレイアウトは、JavaFXを使用してフォームを作成するときに便利です。
パッケージ javafx.scene.layout の GridPane という名前のクラスは、GridPaneを表します。 このクラスは、11のプロパティを提供します-
- alignment -このプロパティはペインの配置を表し、* setAlignment()*メソッドを使用してこのプロパティの値を設定できます。
- hgap -このプロパティはdouble型で、列間の水平方向のギャップを表します。
- vgap -このプロパティはdouble型で、行間の垂直方向のギャップを表します。
- gridLinesVisible -このプロパティはブール型です。 trueの場合、ペインの行は表示されるように設定されます。
以下は、JavaFXのグリッドペイン内のセルの位置です-
(0, 0) | (1, 0) | (2, 0) |
(2, 1) | (1, 1) | (0, 1) |
(2, 2) | (1, 2) | (0, 2) |
例
次のプログラムは、グリッドペインのレイアウトの例です。 これでは、グリッドペインを使用してフォームを作成しています。
このコードを GridPaneExample.java という名前のファイルに保存します。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class GridPaneExample extends Application {
@Override
public void start(Stage stage) {
//creating label email
Text text1 = new Text("Email");
//creating label password
Text text2 = new Text("Password");
//Creating Text Filed for email
TextField textField1 = new TextField();
//Creating Text Filed for password
TextField textField2 = new TextField();
//Creating Buttons
Button button1 = new Button("Submit");
Button button2 = new Button("Clear");
//Creating a Grid Pane
GridPane gridPane = new GridPane();
//Setting size for the pane
gridPane.setMinSize(400, 200);
//Setting the padding
gridPane.setPadding(new Insets(10, 10, 10, 10));
//Setting the vertical and horizontal gaps between the columns
gridPane.setVgap(5);
gridPane.setHgap(5);
//Setting the Grid alignment
gridPane.setAlignment(Pos.CENTER);
//Arranging all the nodes in the grid
gridPane.add(text1, 0, 0);
gridPane.add(textField1, 1, 0);
gridPane.add(text2, 0, 1);
gridPane.add(textField2, 1, 1);
gridPane.add(button1, 0, 2);
gridPane.add(button2, 1, 2);
//Creating a scene object
Scene scene = new Scene(gridPane);
//Setting title to the Stage
stage.setTitle("Grid Pane 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 GridPaneExample.java
java GridPaneExample
実行時に、上記のプログラムは以下に示すようにJavaFXウィンドウを生成します。