tanakahdaのプログラマ手帳

プログラミングとかソフトウェア開発とかの備忘録

Hello JavaFX@JavaFX

Hello.java

package com.tanakahda;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Hello extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("はじめてのJavaFX");
        FXMLLoader loader = new FXMLLoader(getClass().getResource("hello.fxml"));
        HBox root = loader.load();
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

hello.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<HBox>
    <children>
        <Label text="Hello world!" prefWidth="80.0" style="-fx-alignment:center"/>
    </children>
</HBox>

module-info.java

module JavaFXExamples {
    requires transitive javafx.controls;
    requires transitive javafx.fxml;
    opens com.tanakahda;
    exports com.tanakahda;
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>JavaFXExamples</groupId>
    <artifactId>JavaFXExamples</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>17</release>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-controls</artifactId>
                <version>17</version>
        </dependency>
        <dependency>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-fxml</artifactId>
                <version>17</version>
        </dependency>
    </dependencies>
</project>