Springboot整合JavaFX

先看一下效果

在这里插入图片描述

看一下项目目录

在这里插入图片描述

码云源码: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("注册被点击");
    }



}

这样就整合完成了。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot可以很方便地整合JavaFX整合过程主要分为三步: 1. 确定启动入口:推荐使用Spring Boot的命令式启动方式,在启动类中添加以下代码: ```java SpringApplication app = new SpringApplication(XiongdiApplication.class); app.run(args); ``` 这样可以启动Spring Boot应用,然后再启动JavaFX应用。 2. 编写Controller触发按钮事件:在Controller类中,可以使用`@FXML`注解来绑定FXML文件中的组件,如按钮。然后在`initialize`方法中,为按钮添加事件处理器。以下是一个简单的示例代码: ```java @Controller public class LoginController implements Initializable { @FXML private Button loginButton; @Autowired private JdbcTemplate jdbcTemplate; @Override public void initialize(URL url, ResourceBundle resourceBundle) { loginButton.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent actionEvent) { List<Map<String, Object>> res = jdbcTemplate.queryForList("select * from t_user"); if (!res.isEmpty()) { System.out.println(res.toString()); } System.out.println("你点击了我!"); } }); } } ``` 在上述代码中,按钮的点击事件会查询数据库并打印结果。 3. 在JavaFX的启动类中启动Spring Boot应用:添加以下代码到JavaFX的启动类中,以启动Spring Boot应用: ```java @Override public void start(Stage primaryStage) { // 启动Spring Boot应用 SpringApplication app = new SpringApplication(XiongdiApplication.class); ConfigurableApplicationContext context = app.run(); // 启动JavaFX应用 FxMain main = context.getBean(FxMain.class); main.start(primaryStage); } ``` 这样,JavaFX应用启动时会自动启动Spring Boot应用,并且可以使用Spring Boot中的Bean。 这样就完成了Spring BootJavaFX整合。你可以按照上述步骤进行操作,实现Spring BootJavaFX的结合使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值