当前位置: 首页 > news >正文

java8 springboot 集成javaFx 实现一个客户端程序

1. 先创建一个springboot 程序(此步骤不做流程展示)

2. 更改springboot的版本依赖和导入所需依赖

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.7</version><relativePath/> <!-- lookup parent from repository -->
</parent><properties><java.version>1.8</java.version><project.build.jdk>1.8</project.build.jdk>
</properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!--javafx--><dependency><groupId>de.roskenet</groupId><artifactId>springboot-javafx-support</artifactId><version>2.1.6</version></dependency>
</dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><!--打包插件--><plugin><groupId>com.zenjava</groupId><artifactId>javafx-maven-plugin</artifactId><version>8.8.3</version><configuration><vendor>测试</vendor><mainClass>cn.mt4.follow.FollowOrderApplication</mainClass></configuration></plugin></plugins>
</build>

3.创建层级结构

3.1 在resources 目录下创建 fxml(可根据自己习惯更改) 文件夹 用于存放 .fxml 文件
3.2 在XXXApplication 主程序同一层级创建controller(可根据自己习惯更改) 和 view(可根据自己习惯更改) 文件夹

4. 编写代码

4.1 编写.fxml 文件内容(此文件是图形界面内容)
为了快速开发建议安装 JavaFX Scene Builder 2.0 工具,该工具可以进行拖拉拽的形式开发界面.例子:
在这里插入图片描述

图形界面画完之后 可以点击左上角的 File-> Save as 将内容保存为 .fxml 文件 ,并将此文件放到 resources 下的 fxml 文件夹中,博主这里命名为了LoginFxml.fxml ,示例代码如下所示:

<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.control.*?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.*?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"prefWidth="600.0"xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"fx:controller="cn.mt4.follow.controller.LoginController"><children><Label fx:id="accountLabel" layoutX="129.0" layoutY="122.0" text="账号:"><font><Font size="18.0"/></font></Label><Label fx:id="passwordLabel" layoutX="129.0" layoutY="188.0" text="密码:"><font><Font size="18.0"/></font></Label><TextField fx:id="accountField" layoutX="200.0" layoutY="116.0" promptText="请输入账号"><font><Font size="18.0"/></font></TextField><TextField fx:id="passwordField" layoutX="200.0" layoutY="182.0" promptText="请输入密码"><font><Font size="18.0"/></font></TextField><Button fx:id="loginButton" layoutX="335.0" layoutY="283.0" mnemonicParsing="false" text="登录"><font><Font size="18.0"/></font></Button><Button fx:id="resetButton" layoutX="170.0" layoutY="283.0" mnemonicParsing="false" text="重置"><font><Font size="18.0"/></font></Button></children>
</Pane>

注意 JavaFX Scene Builder 2.0 保存的.fxml 文件中是没有 fx:controller 和 fx:id 这两个内容的,需要自己填写.

4.2 分别在view 和 controller 文件夹中创建对应的类
view 类 需要继承 AbstractFxmlView 类,并使用@FXMLView 注解 与 对应的.fxml 文件做绑定.如以下示例:

package cn.mt4.follow.view;import de.felixroske.jfxsupport.AbstractFxmlView;
import de.felixroske.jfxsupport.FXMLView;@FXMLView(value = "/fxml/LoginFxml.fxml")
public class LoginView extends AbstractFxmlView {
}

controller 类 需要 实现 Initializable 接口 ,并重写 initialize 方法,如以下示例:

package cn.mt4.follow.controller;import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;import java.net.URL;
import java.util.ResourceBundle;@FXMLController
public class LoginController implements Initializable {@FXMLprivate TextField accountField;@FXMLprivate TextField passwordField;@FXMLprivate Button loginButton;/*** Called to initialize a controller after its root element has been* completely processed.** @param location  The location used to resolve relative paths for the root object, or*                  <tt>null</tt> if the location is not known.* @param resources The resources used to localize the root object, or <tt>null</tt> if*                  the root object was not localized.*/@Overridepublic void initialize(URL location, ResourceBundle resources) {loginButton.setOnAction(e -> {String accountFieldText = accountField.getText();String passwordFieldText = passwordField.getText();System.out.println("accountFieldText:" + accountFieldText);System.out.println("passwordFieldText:" + passwordFieldText);});}
}

注意: 这里 TextField 和 Button 导入的jar 包是 javafx.scene.control.*
accountField 等属性名与 .fxml 文件中fx:id 定义的名称保持一致

4.3 更改XXXApplication主程序的内容
需要继承 AbstractJavaFxApplicationSupport 类,示例代码:

package cn.mt4.follow;import cn.mt4.follow.view.LoginView;
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import org.springframework.boot.autoconfigure.SpringBootApplication;import java.util.Collection;@SpringBootApplication
public class FollowOrderApplication extends AbstractJavaFxApplicationSupport {public static void main(String[] args) {launch(FollowOrderApplication.class, LoginView.class, args);
//        SpringApplication.run(FollowOrderApplication.class, args);}@Overridepublic void start(Stage stage) throws Exception {stage.setTitle("XXX管理系统");super.start(stage);}@Overridepublic Collection<Image> loadDefaultIcons() {return super.loadDefaultIcons();}
}

至此结束.运行main方法即可运行程序.

正式完成之后需要打包成 可执行文件 ,因在pom 中 导入了 javafx-maven-plugin 插件 ,所以可以在maven中 使用 双击 jfx:native 来打包,运行成功之后会在 target/jfx/native 目录中 生成 .exe 文件 .

http://www.lryc.cn/news/521341.html

相关文章:

  • MySQL(高级特性篇) 06 章——索引的数据结构
  • PanWeidb-使用BenchmarkSQL对磐维数据库进行压测
  • AR 在高校实验室安全教育中的应用
  • 微信小程序实现个人中心页面
  • Spring Boot中的配置文件有哪些类型
  • Spring Boot 项目启动后自动加载系统配置的多种实现方式
  • 如何在 CentOS 中生成 CSR
  • qml XmlListModel详解
  • C++并发编程之跨应用程序与驱动程序的单生产者单消费者队列
  • PostgreSQL技术内幕22:vacuum full 和 vacuum
  • 【网络】:网络编程套接字
  • java基础概念55-不可变集合
  • 深入理解 C++ 函数重载
  • 相机和激光雷达的外参标定 - 无标定板版本
  • Redis 知识速览
  • LeetCode 热题 100_从前序与中序遍历序列构造二叉树(47_105_中等_C++)(二叉树;递归)
  • 使用sqlplus的easy connect时如何指定是链接到shared server还是dedicated process
  • ubuntu22.4 ROS2 安装gazebo(环境变量配置)
  • 【机器学习:十四、TensorFlow与PyTorch的对比分析】
  • [C++]类与对象(上)
  • 大数据技术实训:Zookeeper集群配置
  • HTML5 加载动画(Loading Animation)
  • C语言进阶-2指针(一)
  • 【人工智能】用Python进行对象检测:从OpenCV到YOLO的全面指南
  • 《深度剖析算法优化:提升效率与精度的秘诀》
  • Mysql--重点篇--索引(索引分类,Hash和B-tree索引,聚簇和非聚簇索引,回表查询,覆盖索引,索引工作原理,索引失效,索引创建原则等)
  • matlab使用 BP 神经网络进行数据预测的完整流程,包括数据读取、数据预处理等等
  • systemd-networkd NetworkManager 介绍
  • 本地部署项目管理工具 Leantime 并实现外部访问
  • PHP cURL 函数初学者完全指南