先看一下效果

在这里插入图片描述

看一下项目目录

在这里插入图片描述

码云源码:https://gitee.com/orzdh/springboot-javafx

1、Maven导入Springboot依赖包

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
  </dependency>

2、修改Springboot的启动类

package cn.orz;



import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.net.URL;

@SpringBootApplication
public class SpringboootjavafxApplication extends Application {
    // 任何地方都可以通过这个applicationContext获取springboot的上下文
    public static ConfigurableApplicationContext applicationContext;
    private static String[] args;

    @Override
    public void start(Stage primaryStage) throws Exception {
        URL resource = getClass().getResource("/fxml/Login.fxml");
        if (resource == null) {
            throw new Exception();
        }
        // 加载 fxml 下面的逻辑可以单独封装
        FXMLLoader loader = new FXMLLoader(resource);
        loader.setControllerFactory(new Callback<Class<?>, Object>() {
            @Override
            public Object call(Class<?> param) {
                // 控制器工厂提供bean注入,此处的缺点是不能根据bean名字注入,只能通过class类型注入bean
                // 解决方案:
                // 1、SpringbootJavafxDemoApplication.applicationContext.getBean("Bean Name", Bean.class);
                // 2@Autowired private ApplicationContext applicationContext;
                // Object bean_name = applicationContext.getBean("bean Name", Bean.class);
                return applicationContext.getBean(param);
            }
        });
        // 加载
        GridPane root = loader.load();
        Scene scene = new Scene(root);

        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public static void main(String[] args) {
        SpringboootjavafxApplication.args = args;
        launch(args);
    }

    @Override
    public void init() throws Exception {
        // 启动springboot
        applicationContext = SpringApplication.run(SpringboootjavafxApplication.class, args);
    }

    @Override
    public void stop() throws Exception {
        // 关闭springboot
        applicationContext.stop();
    }
}

3、创建/fxml/Login.fxml文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Hyperlink?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<GridPane alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cn.orz.controller.LoginController">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" maxWidth="145.0" minWidth="10.0" prefWidth="130.0" />
    <ColumnConstraints hgrow="SOMETIMES" maxWidth="455.0" minWidth="10.0" prefWidth="331.0" />
      <ColumnConstraints hgrow="SOMETIMES" maxWidth="195.0" minWidth="10.0" prefWidth="124.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints maxHeight="96.0" minHeight="10.0" prefHeight="45.0" vgrow="SOMETIMES" />
    <RowConstraints maxHeight="96.0" minHeight="10.0" prefHeight="76.0" vgrow="SOMETIMES" />
      <RowConstraints maxHeight="77.0" minHeight="10.0" prefHeight="60.0" vgrow="SOMETIMES" />
      <RowConstraints maxHeight="64.0" minHeight="10.0" prefHeight="59.0" vgrow="SOMETIMES" />
      <RowConstraints maxHeight="156.0" minHeight="10.0" prefHeight="113.0" vgrow="SOMETIMES" />
      <RowConstraints maxHeight="109.0" minHeight="10.0" prefHeight="79.0" vgrow="SOMETIMES" />
  </rowConstraints>
   <children>
      <TextField fx:id="Account" prefHeight="30.0" prefWidth="421.0" promptText="账号" GridPane.columnIndex="1" GridPane.rowIndex="3" />
      <PasswordField fx:id="Password" promptText="密码" GridPane.columnIndex="1" GridPane.rowIndex="4" />
      <AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="5">
         <children>
            <CheckBox fx:id="AutoLogin" layoutX="3.0" layoutY="26.0" mnemonicParsing="false" onAction="#AutoLoginAction" text="自动登录" />
            <CheckBox fx:id="rememberpassword" layoutX="130.0" layoutY="26.0" mnemonicParsing="false" onAction="#RememberPasswordAction" text="记住密码" />
            <Button fx:id="Login" layoutX="1.0" layoutY="66.0" mnemonicParsing="false" onAction="#LoginAction" prefHeight="30.0" prefWidth="336.0" style="-fx-background-color: #585eaa;" text="登录" textFill="WHITE" />
            <Hyperlink fx:id="retrievepassword" layoutX="244.0" layoutY="23.0" onAction="#retrievepasswordAction" text="找回密码" />
         </children>
      </AnchorPane>
      <AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="6">
         <children>
            <Hyperlink fx:id="Register" layoutX="51.0" onAction="#RegisterAction" text="注册账号" />
         </children>
      </AnchorPane>
      <AnchorPane prefHeight="200.0" prefWidth="200.0">
         <children>
            <Label layoutX="68.0" layoutY="10.0" text="登录">
               <font>
                  <Font size="28.0" />
               </font>
            </Label>
         </children>
      </AnchorPane>
   </children>
</GridPane>

4、创建一个Login的控住类 LoginController

package cn.orz.controller;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

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


@Controller
public class LoginController implements Initializable {

    private Logger logger=LoggerFactory.getLogger(LoginController.class);

    @FXML
    private Button Login;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Login.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
             //   List res = jdbcTemplate.queryForList("select * from t_user");
              //  if (!res.isEmpty()) {
                 //   System.out.println(res.toString());
              //  }
                System.out.println("你点击了我!");
            }
        });

    }

    //自动登录单选框
    @FXML
   public  void  AutoLoginAction(){
        logger.info("自动登录单选框被点击");
   }

    @FXML
    public  void  RememberPasswordAction(){
        logger.info("记住密码单选框被点击");
    }

    @FXML
    public  void  LoginAction(){
        logger.info("登录单选框被点击");
    }
    @FXML
    public  void  retrievepasswordAction(){
        logger.info("找回密码连接被点击");
    }
    @FXML
    public  void  RegisterAction(){
        logger.info("注册被点击");
    }



}

这样就整合完成了。

转自:
https://blog.csdn.net/jiulanyangyang/article/details/124819448