背景介绍

技术介绍:

JavaFx: 为java用于GUI(图形化用户界面)方面的技术,用于开发互联网应用程序。
Spring Boot :做Java的都懂,就不再复述了。

集成原因:

JavaFx本身需要手动导入Jar包,且我们开发时需要对于Bean的管理进行关注。如果我们在Spring Boot 中集成JavaFX,那么就可以偷懒了(实际上也便于一直关注web端的Javaer理解和使用)。
在这里插入图片描述

Demo功能:

本次心血来潮,做了个MD5值计算工具,上传文件即可计算文件MD5值。MD5计算,感兴趣的小伙伴可以自行百度,本篇主要讲解FX集成。

项目结构

在这里插入图片描述
此处解释下:resources/vies下的fxml就是fx用来编写桌面的“前端”展示页(view)。相应的后端java下的view目录则是与之绑定的后端。

pom引用

		<!--此包用于支持Spring Boot 集成JavaFX-->
		<dependency>
			<groupId>de.roskenet</groupId>
			<artifactId>springboot-javafx-support</artifactId>
			<version>2.1.6</version>
		</dependency>
		<!--此包用于小工具计算MD5-->
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.4</version>
		</dependency>
		<!--此插件用于fx打包,mainClass为程序入口,注意不要忘填-->
			<plugin>
				<groupId>com.zenjava</groupId>
				<artifactId>javafx-maven-plugin</artifactId>
				<version>8.8.3</version>
				<configuration>
					<mainClass>com.example.demo.DemoApplication</mainClass>
				</configuration>
			</plugin>

代码

贴下代码:
sample.fxml文件

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.FlowPane?>
<!--<FlowPane-->
<!--        xmlns:fx="http://javafx.com/fxml/1"-->
<!--        xmlns="http://javafx.com/javafx/8"-->
<!--        fx:controller="com.example.demo.controller.DemoController">-->
<!--   <children>-->
<!--      <Button fx:id="button" onAction="#click"  text="测试按钮" />-->
<!--   </children>-->
<!--</FlowPane>-->


<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="com.example.demo.controller.DemoController">
<children>
   <Pane prefHeight="324.0" prefWidth="398.0" GridPane.rowIndex="1">
      <children>
         <Button layoutX="28.0" layoutY="33.0" onAction="#finctionSayHello" text="上传文件" />
         <Label fx:id="hello" layoutX="118.0" layoutY="38.0" text="请选择文件" />
         <TextField fx:id="MD5" layoutX="28.0" layoutY="112.0" prefHeight="169.0" prefWidth="362.0" text="文件MD5值:" />
         <Label layoutX="28.0" layoutY="78.0" text="文件MD5值为:" />
      </children>
   </Pane>
</children>
</GridPane>

DemoView.java

package com.example.demo.view;

import de.felixroske.jfxsupport.AbstractFxmlView;
import de.felixroske.jfxsupport.FXMLView;
import javafx.scene.layout.BorderPane;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

/**
 * @author 哈哈
 * @date 2020/6/2  16:55
 */
// fxml的位置相对于resource文件夹
@FXMLView("/view/sample.fxml")
public class DemoView extends AbstractFxmlView {

}

控制器:

package com.example.demo.controller;

import de.felixroske.jfxsupport.FXMLController;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author 哈哈
 * @date 2020/6/2  17:07
 */
@FXMLController
public class DemoController implements Initializable {

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    public Label hello;
    public TextField MD5;

    public void finctionSayHello(ActionEvent actionEvent) {
        hello.setText("文件路径");
        Stage fileStage = null;
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open Resource File");
        //选择文件,识别文件
        File selectedFile =fileChooser.showOpenDialog(fileStage);
        String fileMD5= DigestUtils.md5Hex(String.valueOf(selectedFile));
        hello.setText(String.valueOf(selectedFile));
        System.out.println("文件MD5是"+fileMD5);
        MD5.setText(fileMD5);
    }
}

启动项:

package com.example.demo;

import com.example.demo.splash.DemoSplash;
import com.example.demo.view.DemoView;
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import javafx.stage.Stage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class DemoApplication  extends AbstractJavaFxApplicationSupport  {

	public static void main(String[] args) {
	//对应的启动页面,过场动画
		launch(DemoApplication.class, DemoView.class,new DemoSplash(), args);
	}
	@Override
	public void beforeInitialView(Stage stage, ConfigurableApplicationContext ctx) {
		stage.setTitle("MD5转化工具");
		stage.setWidth(500);
		stage.setHeight(500);
	}

}

动画:因为fx项目启动过程较长,可设置过场动画,也可在启动项中不填,为默认动画。
DemoSplash

package com.example.demo.splash;

import de.felixroske.jfxsupport.SplashScreen;

/**
 * @author 哈哈
 * @date 2020/6/2  17:00
 */
public class DemoSplash extends SplashScreen {
    @Override
    public boolean visible() {
        return super.visible();
    }

    @Override
    public String getImagePath() {
        return super.getImagePath();
    }
}

至此,代码编写完成。

启动效果:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

本代码粘贴可用,创作不易,如果对您有帮助请点赞支持下!!!
我是和弦,注重趣味编程的代码工具人。。。

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐