HelloFX with JavaFX

Introduction

Nice to meet you. This article is the fifth day of ITRC Advent Calendar 2019. This time, I would like to display "Hello, JavaFX" like Hello World in JavaFX.

environment

HelloJavaFX Now let's display HelloJavaFX.

JavaFX download

Download JavaFX to any location from here. This time I downloaded Version 11.0.2. 2019-12-05-171217_1366x768_scrot.png

Eclipse 1 Then open Eclipse and from the menu bar Select Help »Eclipse Marketplace. When you enter "e (fx) clipse" in the search space, it will appear, so install it. 2019-12-05-170957_1366x768_scrot.png

Next, create a new project for JavaFX. This time created with HelloJavaFX. Then create a new package. If you do not create a package and use the default package, it will be difficult later. This time created with hellojavafx. (I heard that using the default package is not so good in the first place) I will create module-info.java later, but I will not create it for now.

2019-12-05-172333_1366x768_scrot.png

Next, unzip the JavaFX you downloaded earlier. If you expand it, you will find two, legal and lib. (It seems that there is also bin on Windows, but there was no Linux)

Go back to Eclipse and right click on the project you created Build Path → Select Build Path Configuration Select the library, select the module path, and select Add External JAR on the right. If you go to the lib that was in the JavaFX you just unzipped, you will find 9 files, so select only the .jar file and press Open.

2019-12-05-172959_1366x768_scrot.png

Now that it's added, apply it and close it. I think the "reference library" has been added to the project and contains the .jar file you just added.

Source code

Next, we will write the code. This time created with HelloFX.java.

package hellojavafx;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class HelloFX extends Application {

	@Override
	public void start(Stage stage) {
		Label label = new Label("Hello, JavaFX");
		label.setFont(new Font(50));
		stage.setScene(new Scene(new StackPane(label), 500, 400));
		stage.show();
	}

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

Contents

Creating a Label

Label label = new Label("Hello, JavaFX");

Font specification

label.setFont(new Font(50));

Set Scene in stage, display Pane in Scene (Label), specify window size (Width, Height)

stage.setScene(new Scene(new StackPane(label), 500, 400));

Show window

stage.show();

Launch application

launch();

Eclipse 2 There is no particular error in the source code, but when I try to run it, an error is displayed on the console and nothing can be done.

error:Main class hellojavafx.Could not detect and load HelloFX
Cause: java.lang.NoClassDefFoundError: javafx/application/Application

Here, create module-info.java that was not created when the project was created. Right click on the project Select Configuration → Create module-info.java The module name can be anything, but make it the same as the project name displayed by default. When it is created, I think the contents are already written.


module HelloJavaFX {
	exports hellojavafx;

	requires javafx.base;
	requires javafx.controls;
	requires javafx.graphics;
}

ʻExportis the release of the package. requires` is the loading of the module.

The reason I didn't create it when creating a new Java project is that it's easier to create it at this timing because there is nothing inside when it is created first.

Let's run it now.

2019-12-05-184831_1366x768_scrot.png

This time I was able to do it well.

As an aside, when I tried to use JavaFX with a person using MAC, the phenomenon that the icon was displayed but the window was not displayed occurred. After investigating, when using it with MAC, it was solved by unchecking the "XstartOnFirstThread" checkbox in the argument of execution configuration. See here

Summary

This time it was like Hello World. If I feel like it, I'll write something after Hello World.

Recommended Posts

HelloFX with JavaFX
[Note] Scene transition with JavaFX
Output characters like conversation with JavaFX
Make a slideshow tool with JavaFX
Hello world with Kotlin and JavaFX
Drag and drop files with JavaFX
Scaling and translation with JavaFX Canvas (Revenge)
JavaFx memorandum
Tool creation with JavaFX & self-contained package (distribution)
Write tests for JavaFX applications with TestFX
Tool creation with JavaFX & self-contained package (JavaFX edition)
Refactored GUI tools made with Java8 + JavaFX in 2016
Hello World with JavaFX 11 (OpenJFX) in Liberica JDK 11
A simple rock-paper-scissors game with JavaFX and SceneBuilder