JavaFX的目录结构, 项目创建和发布, 基于JDK11+JavaFX SDK17
JDK 和 JavaFX SDK
需要使用JDK11, 推荐使用 https://adoptium.net/releases.html
JDK11
JavaFX 11 不再是JDK的一部分, 需要单独安装, 或者直接通过Maven Dependency引入.
参考 https://stackoverflow.com/questions/52467561/intellij-cant-recognize-javafx-11-with-openjdk-11
Start Guide: https://openjfx.io/openjfx-docs/#introduction
- JavaFX 11 is not part of the JDK anymore
- You can get it in different flavors, either as an SDK or as regular dependencies (maven/gradle).
- You will need to include it to the module path of your project, even if your project is not modular.
如果不使用Maven, 需要在 https://gluonhq.com/products/javafx/ 下载对应版本的sdk,
JDK9之后的模块化
https://www.oracle.com/corporate/features/understanding-java-9-modules.html
不使用 Maven 创建 JavaFX 项目
- IDEA 直接用菜单新建JavaFX项目, 但是这种只适合 JDK8
- 如何在IDEA下创建JavaFX项目的说明 https://openjfx.io/openjfx-docs/#IDE-Intellij
例子
使用 Maven 创建JavaFX项目
使用 Maven 创建 JavaFX 项目是较简单方便的一种方式, 不需要关心包依赖关系, 只需要手工初始化一个项目结构, 剩下的事都可以交给Maven处理.
1. 项目结构
项目结构如下, 其中resources目录下的资源文件, 可以放在 resources 根目录, 也可以放到resources/org/openjfx, 两者在App.java中的载入方式不同
├── javafx_test01
│ ├── pom.xml
│ ├── src
│ │ └── main
│ │ ├── java
│ │ │ ├── com
│ │ │ │ └── rockbb
│ │ │ │ ├── App.java
│ │ │ │ ├── PrimaryController.java
│ │ │ │ └── SecondaryController.java
│ │ │ └── module-info.java
│ │ └── resources
│ │ └── com
│ │ └── rockbb
│ │ ├── primary.fxml
│ │ ├── secondary.fxml
│ │ └── styles.css
└── settings.xml
2. pom.xml
指定JDK版本为11, javafx版本为17.0.1, javafx.maven.plugin使用最新的0.0.8
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rockbb</groupId>
<artifactId>javafx-test01</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>11</maven.compiler.release>
<javafx.version>17.0.1</javafx.version>
<javafx.maven.plugin.version>0.0.8</javafx.maven.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>${maven.compiler.release}</release>
<source>${maven.compiler.release}</source>
<target>${maven.compiler.release}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>${javafx.maven.plugin.version}</version>
<configuration>
<jlinkImageName>hellofx</jlinkImageName>
<launcher>launcher</launcher>
<mainClass>hellofx/org.openjfx.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. module-info.java
这里定义项目模块的可见度, 反射的可见度, 以及依赖的其他模块. 后面的opens ... to 和 exports 需要使用自己工程的包名
module hellofx {
requires javafx.controls;
requires javafx.fxml;
opens com.rockbb to javafx.fxml;
exports com.rockbb;
}
4. App.java
这是应用的入口. 下面的载入方式对应资源文件在根目录, 如果要按 package 放, 去掉其中的.getClassLoader()就可以了
package com.rockbb;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class App extends Application {
private static Scene scene;
@Override
public void start(Stage stage) throws IOException {
scene = new Scene(loadFXML("primary"));
scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());
stage.setScene(scene);
stage.show();
}
static void setRoot(String fxml) throws IOException {
scene.setRoot(loadFXML(fxml));
}
private static Parent loadFXML(String fxml) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
return fxmlLoader.load();
}
public static void main(String[] args) {
launch();
}
}
5. PrimaryController.java
package com.rockbb;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class PrimaryController {
public Button primaryButton;
@FXML
private void switchToSecondary() throws IOException {
App.setRoot("secondary");
}
}
6. SecondaryController.java
package com.rockbb;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class SecondaryController {
public Button secondaryButton;
@FXML
private void switchToPrimary() throws IOException {
App.setRoot("primary");
}
}
7. primary.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>
<VBox alignment="CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.rockbb.PrimaryController">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
</padding>
<Label text="Primary View"/>
<Button fx:id="primaryButton" text="Switch to Secondary View" onAction="#switchToSecondary"/>
</VBox>
8. secondary.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>
<VBox alignment="CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.rockbb.SecondaryController">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
</padding>
<Label text="Secondary View"/>
<Button fx:id="secondaryButton" text="Switch to Primary View" onAction="#switchToPrimary"/>
</VBox>
8. styles.css
.button {
-fx-text-fill: blue;
}
运行
IDEA中在App类上右键菜单, 点Run即可运行
打包发布
在JDK16之前, 可以使用jlink将项目打包为带目录结构的可执行文件, 在pom中修改javafx-maven-plugin的配置
<build>
<plugins>
...
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>${javafx.maven.plugin.version}</version>
<configuration>
<!-- 指定jlink路径,如果你的系统中默认路径是其他版本的jdk, 就必须用这个参数指定 -->
<jlinkExecutable>/opt/jdk/jdk-11.0.12/bin/jlink</jlinkExecutable>
<!-- 压缩比例, 值可以为0,1,2, 默认为0 -->
<compress>2</compress>
<!-- Remove the includes directory in the resulting runtime image -->
<noHeaderFiles>true</noHeaderFiles>
<!-- Strips debug information out -->
<stripDebug>true</stripDebug>
<!-- Remove the man directory in the resulting Java runtime image -->
<noManPages>true</noManPages>
<!-- Add a launcher script -->
<launcher>launcher</launcher>
<!-- what main needs to be launched by specifying module, package and class -->
<mainClass>hellofx/com.rockbb.App</mainClass>
<!-- The name of the folder with the resulting runtime image -->
<jlinkImageName>hellofx</jlinkImageName>
<!-- When set, creates a zip of the resulting runtime image -->
<jlinkZipName>hellofx</jlinkZipName>
</configuration>
</plugin>
...
</plugins>
</build>
执行打包
clean compile javafx:jlink -f pom.xml
压缩使用2时, 最终产生的lib/modules尺寸会明显小很多, 这个并不一定体现到zip包的大小上, 2产生的zip包可能比0更大
在JDK16之后, 可以使用jpackage.
参考
- 英文教程 http://tutorials.jenkov.com/javafx/index.html
- 打包为fatjar https://stackoverflow.com/questions/52653836/maven-shade-javafx-runtime-components-are-missing
- jlink和jpackage打包 https://dev.to/cherrychain/javafx-jlink-and-jpackage-h9
JavaFX的目录结构, 项目创建和发布, 基于JDK11+JavaFX SDK17的更多相关文章
- linux专题一之文件管理(目录结构、创建、查看、删除、移动)
在linux系统中一切都是文件./ 在linux中为根目录,是一切文件的根目录.本文将通过linux系统的目录结构和与linux文件操作有关的相关命令(touch.mkdir.cp.mv.mv.les ...
- Oracle目录结构及创建新数据库
oracle目录结构 当需要创建新的数据仓库时我可以用 Database Configuration Assistant(数据库配置助手) admin 存放创建的不同数据库 cfgtoollogs c ...
- JavaWeb之基础(1) —— 文件、目录结构和创建项目
1. JavaWeb应用 JavaWeb应用从大类上分为静态和动态两种. 静态应用就是传统的HTML文件+素材资源构造的静态网页,不需要特殊的配置.JavaWeb也不是专门用来做静态网站的. 动态应用 ...
- Linu目录结构和创建用户
具体目标结构 ./bin [重点] (/usr/bin./usr/local/bin) ●是Binary的速写,这个目录存放着最经常使用的命令. ./sbin (/usr/sbin./usr/loca ...
- 从0开始,手把手教你用Vue开发一个答题App01之项目创建及答题设置页面开发
项目演示 项目演示 项目源码 项目源码 教程说明 本教程适合对Vue基础知识有一点了解,但不懂得综合运用,还未曾使用Vue从头开发过一个小型App的读者.本教程不对所有的Vue知识点进行讲解,而是手把 ...
- iOS开发总结——项目目录结构
1.前言 清晰的项目目录结构有利于项目的开发,同时也是软件架构的一部分,所以,项目开发之初搭建项目的目录结构很重要.刚转iOS时,自己并不知道如何搭建App的项目目录,在参与开发两个应用后,结合Web ...
- Angular项目目录结构
前言:不支持MakeDown的博客园调格式的话,真的写到快o(╥﹏╥)o了,所以老夫还是转战到CSDN吧,这边就不更新啦啦啦~ CSDN地址:https://blog.csdn.net/Night20 ...
- Node.js 文件夹目录结构创建
第一次接触NodeJS的文件系统就被它的异步的响应给搞晕了,后来发现NodeJS判断文件夹是否存在和创建文件夹是还有同步方法的,但是还是想尝试使用异步的方法去实现. 使用的方法:fs.exists(p ...
- NET5实践:项目创建-结构概述-程序运行-发布部署
ASP.NET5实践01:项目创建-结构概述-程序运行-发布部署 1.项目创建 ASP.NET5项目模板有三种: 新建项目: 选择模板: 2.结构概述 References对应配置是project ...
- maven 创建web项目的标准目录结构
maven 创建web项目的标准目录结构 CreateTime--2018年4月18日21:05:37 Author:Marydon 1.标准目录介绍(开发目录) 2.在eclipse下,目录展示 ...
随机推荐
- 使用QQ屏幕识图实现识别表格功能
1.问题 目前市场上的OCR工具对于识别表格功能均是采取了收费制度,但我们时常要进行一些表格的复制(原表格为图片) 便可以使用QQ或钉钉自带的功能来实现 2.解决 1.QQ屏幕识图 先使用屏幕识图功能 ...
- P5707 【深基2.例12】上学迟到
1.题目介绍 2.题解 这里只有两个稍微注意的点 2.1 s % v != 0(向上取整) 这里的话,若是结果不为整数,我们必须向上取整,必须保证空余时间永远大于所需时间! 2.2 ceil向上取整函 ...
- 例2.9 建立一个带头结点的线性链表,用以存放输人的二进制数,链表中每个结点的data域存放一个二进制位。并在此链表上实现对二进制数加1的运算。
1.题目 例2.9建立一个带头结点的线性链表,用以存放输人的二进制数,链表中每个结点的data域存放一个二进制位.并在此链表上实现对二进制数加1的运算. 2.算法分析 3.代码 /* 二进制加1 */ ...
- Nginx日志规则以及根据日志进行性能问题判断的思路
Nginx日志规则以及根据日志进行性能问题判断的思路 背景 Nginx是开源方案里面能实现反向代理 负载均衡的首选. 但是有时候性能出问题比较难以分析和定位, 不知道是不是nginx的瓶颈 性能问题的 ...
- [转帖]为非root用户添加NOPASSWD权限
https://www.jianshu.com/p/d1e71bda4b34 查看树莓派默认是怎么为pi用户免去密码 所有配置文件都在 /etc 目录下,免去密码配置文件也不例外.在/etc/sudo ...
- [转帖]Linux | crontab定时任务及开机自启项
本文总结如何使用 crontab 创建定时任务及开启自启项 1. 本文基于 CentOS 7.9 系统所写. Updated: 2022 / 9 / 5 Linux | crontab定时任务及开机自 ...
- [转帖]JVM 问题诊断快速入门
https://zhuanlan.zhihu.com/p/110197145 JVM 全称为 Java Virtual Machine,翻译为中文 "Java 虚拟机".本文中的J ...
- vue3封装搜索表单组件
seacrch 表单完成的功能 1.根据配置json配置项自动生成表单 ok 2.是响应式的排版 ok 3.点击搜索按钮会向上抛出值 ok 4.点击重置按钮会自动清空数据,不需要父组件额外的处理 ok ...
- vue写组件时的命名规范
1组件命名驼峰 如myBread.vue(组件) 2引入时,接受同样是驼峰 import MyBread from "@/components/cuscom/myBread.vue" ...
- vue动画 <transition-group> 使用半场动画
<style> /* 给动画添加一组过度效果 */ .v-enter, .v-leave-to { opacity: 0.8; /* 从右向左进入 */ transform: transl ...