Javafx-layout-stackpane

提供:Dev Guides
移動先:案内検索

JavaFX-レイアウトスタックペイン

スタックペインを使用する場合、ノードはスタックと同じように別のノードの上に配置されます。 最初に追加されたノードはスタックの下部に配置され、次のノードはスタックの上部に配置されます。

パッケージ javafx.scene.layoutStackPane というクラスは、StackPaneを表します。 このクラスには、alignmentという名前の単一のプロパティが含まれます。 このプロパティは、スタックペイン内のノードの配置を表します。

これらに加えて、このクラスは* setMargin()*という名前のメソッドも提供します。 このメソッドは、スタックペイン内のノードのマージンを設定するために使用されます。

次のプログラムは、 StackPane レイアウトの例です。 ここでは、円、球、テキストを同じ順序で挿入しています。

このコードを StackPaneExample.java という名前のファイルに保存します。

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;

import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Sphere;

import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class StackPaneExample extends Application {
   @Override
   public void start(Stage stage) {
     //Drawing a Circle
      Circle circle = new Circle(300, 135, 100);
      circle.setFill(Color.DARKSLATEBLUE);
      circle.setStroke(Color.BLACK);

     //Drawing Sphere
      Sphere sphere = new Sphere(50);

     //Creating a text
      Text text = new Text("Hello how are you");

     //Setting the font of the text
      text.setFont(Font.font(null, FontWeight.BOLD, 15));

     //Setting the color of the text
      text.setFill(Color.CRIMSON);

     //setting the position of the text
      text.setX(20);
      text.setY(50);

     //Creating a Stackpane
      StackPane stackPane = new StackPane();

     //Setting the margin for the circle
      stackPane.setMargin(circle, new Insets(50, 50, 50, 50));

     //Retrieving the observable list of the Stack Pane
      ObservableList list = stackPane.getChildren();

     //Adding all the nodes to the pane
      list.addAll(circle, sphere, text);

     //Creating a scene object
      Scene scene = new Scene(stackPane);

     //Setting title to the Stage
      stage.setTitle("Stack 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 StackPaneExample.java
java StackPaneExample

実行時に、上記のプログラムは以下に示すようにJavaFXウィンドウを生成します。

StackPane