ヤマカサのプログラミング勉強日記

プログラミングに関する日記とどうでもよい雑記からなるブログです。

JavaFX ウィンドウサイズの疑問?

JavaFXでウィンドウサイズを取得する

疑問が出たのでメモします。

コード

package application;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Test0 extends Application{
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage stage) throws Exception{
        stage.setTitle("Test0");
        stage.setHeight(300);
        stage.setWidth(300);

        // ウィンドウサイズをラベルで表示させる
        Label label1 = new Label();
        label1.textProperty().bind(stage.widthProperty().asString());
        label1.setFont(new Font(20));
        Label wid = new Label("Width: ");
        wid.setFont(new Font(20));
        
        HBox hbox1 = new HBox(wid, label1);
        hbox1.setLayoutX(20);
        hbox1.setLayoutY(40);

        Label label2 = new Label();
        label2.textProperty().bind(stage.heightProperty().asString());
        label2.setFont(new Font(20));
        Label hei = new Label("Height: ");
        hei.setFont(new Font(20));
        
        HBox hbox2 = new HBox(hei, label2);
        hbox2.setLayoutX(20);
        hbox2.setLayoutY(100);

        Rectangle rect1 = new Rectangle(0, 0, 150, 150);
        rect1.setStroke(Color.BLACK);
        rect1.setArcWidth(20.0);
        rect1.setArcHeight(20.0);
        rect1.setFill(null);
        
        Group g = new Group(hbox1, hbox2, rect1);

        Scene scene = new Scene(g);
        stage.setScene(scene);
        stage.show();
    }
}