曦远 (DealiAxy)一份私人的文字刊物

StarBlog
返回文章列表折页手记阅读全文
Java

Configuring JavaFX Development on Linux-Ubuntu (Updated)

Preface I'm not sure why, but there is very little Chinese material about JavaFX online, and it's all quite old, so I suggest that those interested in learning JavaFX would be better off reading the official documentation. Also, it seems some people don't think highly of JavaFX. There's actually a lot more material about Swing. I find that rather strange—from a design perspective, JavaFX is more sophisticated, isn't it? PS: After nearly a week of tinkering and exploring, I found that using JavaFX with OpenJDK really has a lot of pitfalls~ The configuration of OpenJDK and Oracle JDK is different; here rec

曦远 (DealiAxy)约 11 分钟
目录7 个章节

Preface

I'm not sure why, but there is very little Chinese material about JavaFX online, and it's all quite old, so I suggest that those interested in learning JavaFX would be better off reading the official documentation. Also, it seems some people don't think highly of JavaFX. There's actually a lot more material about Swing. I find that rather strange—from a design perspective, JavaFX is more sophisticated, isn't it?

PS: After nearly a week of tinkering and exploring, I found that using JavaFX with OpenJDK really has a lot of pitfalls~ The configuration of OpenJDK and Oracle JDK is different; I recommend using Oracle JDK to avoid running into weird pitfalls~

Here's How to Install Oracle JDK

$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer

OpenJDK Requires One Extra Step

Because OpenJDK 8 does not include the JavaFX package by default, you need to install OpenJFX first.

Configuration

Open IDEA; in a regular Java project, importing the JavaFX package won't find it. When creating a Java project, IDEA does not import the JavaFX package by default, so we need to find the path ourselves and import it.

Open Project Structure, select SDKs, and you can see the JDK path.

Click the “+” sign next to it, then in the JDK directory, find the jfxrt.jar package in the /lib/ext folder and import it, and you're good to go~

HelloWorld

The first program starts with HelloWorld!

package lin.Learning.JavaFx;

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


public class HelloWorld extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        Label label = new Label("the first label");
        // 创建面板作为根节点
        StackPane rootNode=new StackPane();
        // 将label控件添加到根节点上
        rootNode.getChildren().add(label);
        // 创建场景对象,指定根节点对象和大小
        Scene scene=new Scene(rootNode,200,60);
        primaryStage.setTitle("Hello JavaFx");
        // 将场景添加到舞台中
        primaryStage.setScene(scene);
        // 显示舞台
        primaryStage.show();
    }
}

Running result:

And with that, you've completed your first JavaFX program development on Linux.

Installing Scene Builder

A distinctive feature of JavaFX is the separation of View and Controller, using FXML to write the UI layout, and Oracle provides a WYSIWYG UI design tool, though you have to install it yourself.

Download URL: https://www.oracle.com/technetwork/java/javase/downloads/javafxscenebuilder-1x-archive-2199384.html#javafx-scenebuilder-2.0-oth-JPR

Just download the version for your system; here I chose the Debian deb package.

For installing Scene Builder, you can refer to Oracle's documentation: https://docs.oracle.com/javase/8/scene-builder-2/installation-guide/preface.htm#sthref2

Here's an excerpt from the documentation about installing Scene Builder on Linux:

(Linux platform) Extract the Scene Builder 2.0 files from the javafx_scenebuilder-2_0-linux-<platform>.tar.gz to a directory on your local file system, or double-click the javafx_scenebuilder-2_0-linux-<platform>.deb file to open it with Ubuntu Software Center, where <platform> is either x64 or i586. By default, the Scene Builder application is installed at /opt/JavaFXSceneBuilder2.0/.

As you can see, the Scene Builder installation directory is: /opt/JavaFXSceneBuilder2.0/

Next, we need to configure it in IDEA.

IDEA Configuration

Open Settings

Configure the path

Then add an FXML file to our project. Right-click and you can open it with Scene Builder; the interface is pretty nice.

_EOF_

Configuring JavaFX Development on Linux-Ubuntu (Updated)

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

Java开发:JDK安装和环境配置

前言 环境配置是开发中最基础的部分,不过最近有很多新入坑的同学,都在问怎么安装JDK和配置环境,索性写一篇。 这里要注意一下,在Windows平台和Linux平台下做开发是不同的,Windows平台开发Java需要自己手动配置JDK的环境变量,而大多数Linux都是不需要的,安装了OpenJDK就可以了(可能很多发行版都自带了OpenJDK),这点是比较方便的。 下载Java JDK Window

2023-02-08 · Java · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…