Javafx-layout-anchorpane
提供:Dev Guides
JavaFX-アンカーのレイアウト
アンカーペインにより、子ノードのエッジをアンカーペインのエッジからのオフセットに固定できます。 アンカーペインに境界線やパディングセットがある場合、オフセットはこれらのインセットの内側の端から測定されます。
アプリケーションでアンカーペインを使用する場合、そのペイン内のノードはペインから特定の距離に固定されます。
パッケージ javafx.scene.layout の AnchorPane という名前のクラスは、アンカーペインを表します。 ノードを追加したら、ペインの境界からすべての方向(上、下、右、左)にアンカーを設定する必要があります。 アンカーを設定するために、このクラスには4つのメソッドがあります。* setBottomAnchor()、setTopAnchor()、setLeftAnchor()、setRightAnchor()*です。 これらのメソッドには、アンカーを表すdouble値を渡す必要があります。
例
次のプログラムは、アンカーペインレイアウトの例です。 この例では、アンカーペインに回転シリンダーを挿入しています。 同時に、すべての方向(上、左、右、下)からペインから50単位の距離に設定します。
このコードを AnchorPaneExample.java という名前のファイルに保存します。
import javafx.animation.RotateTransition;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Cylinder;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
public class AnchorPaneExample extends Application {
@Override
public void start(Stage stage) {
//Drawing a Cylinder
Cylinder cylinder = new Cylinder();
//Setting the properties of the Cylinder
cylinder.setHeight(180.0f);
cylinder.setRadius(100.0f);
//Preparing the phong material of type diffuse color
PhongMaterial material = new PhongMaterial();
material.setDiffuseColor(Color.BLANCHEDALMOND);
//Setting the diffuse color material to Cylinder5
cylinder.setMaterial(material);
//Setting rotation transition for the cylinder
RotateTransition rotateTransition = new RotateTransition();
//Setting the duration for the transition
rotateTransition.setDuration(Duration.millis(1000));
//Setting the node for the transition
rotateTransition.setNode(cylinder);
//Setting the axis of the rotation
rotateTransition.setAxis(Rotate.X_AXIS);
//Setting the angle of the rotation
rotateTransition.setByAngle(360);
//Setting the cycle count for the transition
rotateTransition.setCycleCount(RotateTransition.INDEFINITE);
//Setting auto reverse value to false
rotateTransition.setAutoReverse(false);
//playing the animation
rotateTransition.play();
//Creating an Anchor Pane
AnchorPane anchorPane = new AnchorPane();
//Setting the anchor to the cylinder
AnchorPane.setTopAnchor(cylinder, 50.0);
AnchorPane.setLeftAnchor(cylinder, 50.0);
AnchorPane.setRightAnchor(cylinder, 50.0);
AnchorPane.setBottomAnchor(cylinder, 50.0);
//Retrieving the observable list of the Anchor Pane
ObservableList list = anchorPane.getChildren();
//Adding cylinder to the pane
list.addAll(cylinder);
//Creating a scene object
Scene scene = new Scene(anchorPane);
//Setting title to the Stage
stage.setTitle("Anchor 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 AnchorPaneExample.java
java AnchorPaneExample
実行時に、上記のプログラムは以下に示すようにJavaFXウィンドウを生成します。