下面来介绍创建maven的javaFX+springboot项目,基于用户界面与后天逻辑分离的方式,用户界面使用fxml文件来常见,类似于jsp,可以引入css文件修饰界面

maven依赖

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>de.roskenet</groupId>
<artifactId>springboot-javafx-support</artifactId>
<version>${springboot-javafx-support.version}</version>
</dependency>
  • 创建login.fxml文件,将文件放入resources下,因为springboot默认加载的资源为resources
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?> <AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="588.0" prefWidth="802.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2"
fx:controller="com.dome.controller.LoginController">
<children>
<HBox layoutX="122.0" layoutY="108.0" prefHeight="372.0" prefWidth="526.0">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" prefHeight="30.0" prefWidth="70.0" text="用户名" textAlignment="LEFT">
<font>
<Font size="18.0" fx:id="x1" />
</font>
<HBox.margin>
<Insets bottom="10.0" left="50.0" right="10.0" top="100.0" />
</HBox.margin>
</Label>
<TextField fx:id="userName" id="username" prefHeight="30.0" prefWidth="200.0">
<HBox.margin>
<Insets bottom="10.0" right="10.0" top="100.0" />
</HBox.margin>
</TextField>
<Label alignment="CENTER" contentDisplay="CENTER" font="$x1" prefHeight="30.0" prefWidth="70.0" text="密 码" textAlignment="LEFT">
<HBox.margin>
<Insets bottom="10.0" left="-290.0" top="140.0" />
</HBox.margin>
</Label>
<TextField fx:id="passWord" id="password" prefHeight="30.0" prefWidth="200.0">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="140.0" />
</HBox.margin>
</TextField>
<Button id="region" contentDisplay="CENTER" font="$x1" minHeight="28.0" mnemonicParsing="false" opacity="0.79" prefHeight="35.0" prefWidth="75.0" text="注册" textAlignment="CENTER">
<HBox.margin>
<Insets bottom="10.0" left="-240.0" top="200.0" />
</HBox.margin>
</Button>
<Button id="login" font="$x1" minHeight="28.0" mnemonicParsing="false" opacity="0.79" prefHeight="35.0" prefWidth="75.0"
text="登录" onAction="#btnClick" >
<HBox.margin>
<Insets bottom="10.0" left="60.0" top="200.0" />
</HBox.margin>
</Button>
</children>
</HBox>
</children>
</AnchorPane>
  • 创建LoginFXML类,指定fxml文件的路径,及引入fxml文件所需的样式
import de.felixroske.jfxsupport.AbstractFxmlView;
import de.felixroske.jfxsupport.FXMLView; @FXMLView(value = "/static/fxml/login.fxml", css = {"/static/style/login.css"},title = "用户登录")
public class LoginFXML extends AbstractFxmlView { }

可以来了解下FXMLView的参数

@Component
@Retention(RetentionPolicy.RUNTIME)
public @interface FXMLView {
String value() default ""; String[] css() default {}; String bundle() default ""; String title() default ""; String stageStyle() default "UTILITY";
}
  • 创建LoginController,与用户界面进行交互
import com.dome.MainController;
import com.dome.domain.Student;
import com.dome.service.IStudentService;
import com.dome.view.LoginFXML;
import de.felixroske.jfxsupport.FXMLController;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import org.springframework.beans.factory.annotation.Autowired; import java.net.URL;
import java.util.List;
import java.util.ResourceBundle; @FXMLController
public class LoginController implements Initializable { @FXML
private Button button;
@FXML
private TextField userName; @Autowired
private IStudentService studentService; private ResourceBundle resourceBundle; @Override
public void initialize(URL location, ResourceBundle resources) {
resourceBundle = resources;
} @FXML
public void btnClick(ActionEvent actionEvent) {
List<Student> students = studentService.listAll();
userName.setText("helloWorld");
} @FXML
public void btnLoginClick(ActionEvent actionEvent) {
MainController.showView(LoginFXML.class);
} }
  • 创建main方法,启动项目
import com.dome.view.ExportClassEntityFXML;
import com.dome.view.LoginFXML;
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import javafx.stage.Stage;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan; /**
* maven构建JavaFX+SpringBoot项目启动类
*/
@ComponentScan({"com.dome.view","com.dome.controller","com.dome.service"})
@SpringBootApplication
public class MainController extends AbstractJavaFxApplicationSupport { public static void main(String[] args) {
launch(MainController.class, ExportClassEntityFXML.class, args);
} @Override
public void start(Stage stage) throws Exception {
super.start(stage);
stage.setTitle("用户登录");
//窗口最大化显示
// Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
// stage.setX(primaryScreenBounds.getMinX());
// stage.setY(primaryScreenBounds.getMinY());
// stage.setWidth(primaryScreenBounds.getWidth());
// stage.setHeight(primaryScreenBounds.getHeight());
// stage.setMaximized(true);//设置窗口最大化
// stage.setFullScreen(true);//全屏显示,Esc退出
// stage.setAlwaysOnTop(true);//始终显示在其他窗口之上
}
}

其中ComponentScan用来设置扫描包的中的类。

最后项目结构入下:

(四)创建基于maven的javaFX+springboot项目,用户界面与后台逻辑分离方式的更多相关文章

  1. (三)创建基于maven的javaFX+springboot项目创建

    创建基于maven的javaFx+springboot项目有两种方式,第一种为通过非编码的方式来设计UI集成springboot:第二种为分离用户界面(UI)和后端逻辑集成springboot,其中用 ...

  2. (二)创建基于maven的javaFX项目

    首先使用IDEA创建一个javaFX项目 点击finish,这就创建完成了JavaFX项目,只有将其转换为maven项目即可,如图:

  3. 使用IDEA创建基于Maven SpringMvc项目

    使用IDEA创建基于Maven SpringMvc项目 1.通过程序启动——create project,或者file--New-projec打开New project 2.自定义groupid等信息 ...

  4. 转:基于Maven管理的JavaWeb项目目录结构参考

    通常在创建JavaWeb项目时多多少少都会遵循一些既定的比较通用的目录结构,下面分享一张基于Maven管理的JavaWeb项目目录结构参考图: 上图仅是参考,不同项目不同团队都有自己的约定和规范. 个 ...

  5. IntelliJ IDEA基于maven构建的web项目找不到jar包

    基于maven构建的springMVC项目,下载好jar包import后,运行提示ClassNotFoundException: java.lang.ClassNotFoundException: o ...

  6. 创建基于maven的项目模版

    我们在实际工作中 ,有些项目的架构是相似的,例如基于 restful的接口项目,如果每次都重新搭建一套架构或者通过拷贝建立一个项目难免有些得不偿失,这里我们可以用maven的archtype建立项目模 ...

  7. 从头开始基于Maven搭建SpringMVC+Mybatis项目(3)

    接上文内容,本节介绍基于Mybatis的查询和分页功能,并展示一个自定义的分页标签,可重复使用以简化JSP页面的开发. 从头阅读传送门 在上一节中,我们已经使用Maven搭建好了项目的基础结构,包括一 ...

  8. Springboot 创建的maven获取resource资源下的文件的两种方式

    Springboot 创建的maven项目 打包后获取resource下的资源文件的两种方式: 资源目录: resources/config/wordFileXml/wordFileRecord.xm ...

  9. 学习笔记——Maven实战(四)基于Maven的持续集成实践

    Martin的<持续集成> 相信很多读者和我一样,最早接触到持续集成的概念是来自Martin的著名文章<持续集成>,该文最早发布于2000年9月,之后在2006年进行了一次修订 ...

随机推荐

  1. antd源码分析之——栅格(Grid)

    官方文档 https://ant.design/components/grid-cn/ 目录 一.antd中的Grid 代码目录 1.整体思路 2.less文件结构图(♦♦♦重要) 3.less实现逻 ...

  2. DataSet转换为实体类

    /// <summary> /// DataSet转换为实体类 /// </summary> /// <typeparam name="T">实 ...

  3. qt5之网络通信

    QT5 TCP网络通讯 服务器与客户端建立连接listen() - connectToHost();  触发newPendingConnect信号 实时数据通讯write(); read();  触发 ...

  4. linux下如何单独编译设备树?

    答: make <vendor>/<device_name>.dtb 如: make freescale/fsl-1043a-rdb.dtb

  5. C++ STL——异常

    目录 一 C++异常机制概述 二 栈解旋(unwinding) 三 异常接口的声明 四 异常类型和异常变量的生命周期 五 C++标准异常库 六 异常的继承 注:原创不易,转载请务必注明原作者和出处,感 ...

  6. 把文档中的数据取出并插入到excel中

    from xlrd import open_workbookfrom xlutils.copy import copy jsonfile=r'C:\Users\Administrator\Deskto ...

  7. HTTP请求协议中请求报文(Request Headers)跟响应报文(Response Headers)的简单理解

    背景 今儿个一新来的应届生问我,开发模式中所看到的web请求的请求头里的属性怎么理解,我便根据自己的经验随便拉开一个请求跟他聊了起来,顺便自己记录下文字版,以后再有交流直接发地址给他就好了,嘻嘻,机智 ...

  8. Kafka offset机制

  9. 手写web框架之实现依赖注入功能

    我们在Controller中定义了Service成员变量,然后在Controller的Action方法中调用Service成员变量的方法,那么如果实现Service的成员变量? 之前定义了@Injec ...

  10. 包含min函数的栈、队列

    题目:定义栈的数据结构,请在该类型中实现一个能够得到栈/队列的最小元素的min函数.在该栈/队列中,调用min.入栈(入队列)及出栈(出队列)函数的时间复杂度都是O(1). 1. 包含min函数的栈 ...