背景介紹
技術介紹:
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();
}
}
至此,代碼編寫完成。
啓動效果:
本代碼粘貼可用,創作不易,如果對您有幫助請點贊支持下!!!
我是和絃,注重趣味編程的代碼工具人。。。