欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-12-21 20:27:45
下面来介绍创建maven的javaFX+springboot项目,基于用户界面与后天逻辑分离的方式,用户界面使用fxml文件来常见,类似于jsp,可以引入css文件修饰界面 maven依赖 创建login.fxml文件,将文件放入resources下,因为springboot默认加载的资源为reso ......

下面来介绍创建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项目,用户界面与后台逻辑分离方式