SpringBoot整合JavaFx(⼗三)
SpringBoot整合JavaFx(⼗三)
在Java中,基本上万物可springboot… 整合了spring全家桶,你可以很⽅便整合它的⽣态框架。
JavaFx也能整合springboot,下⾯我就演⽰javafx+springboot操作数据库吧,学习了下⾯的⽅式,针对其他main⼯程也适⽤。整合过程主要分三步:
1、引⼊springboot依赖
2、配置
3、加载fxml时注⼊bean
特别注意第三点,使⽤javafx提供的控制器⼯⼚能轻松注⼊。开发模式类似MVC,只不过视图变成了fxml。
⼀、配置
⽤IDEA初始化⼀个springboot项⽬,,Maven依赖如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"xsi="/2001/XMLSchema-instance"
schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.lingkang</groupId>
<artifactId>springboot-javafx-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-javafx-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
springboot架构图
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>top.lingkang.springbootjavafxdemo.SpringbootJavafxDemoApplication</mainClass> </configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
配置application.properties
spring.main.web-application-type=none
spring.main.allow-bean-definition-overriding=true
# 应⽤名称
spring.application.name=springboot-javafx-demo
# 数据库驱动:
spring.datasource.sql.cj.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC # 数据库⽤户名&密码:
spring.datasource.username=root
spring.datasource.password=123456
⼆、编写启动类
import Application;
import FXMLLoader;
import Scene;
import VBox;
import Stage;
import Callback;
import SpringApplication;
import SpringBootApplication;
import ConfigurableApplicationContext;
import URL;
@SpringBootApplication
public class SpringbootJavafxDemoApplication 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、Bean("Bean Name", Bean.class);
// 2、@Autowired private ApplicationContext applicationContext;
// Object bean_name = Bean("bean Name", Bean.class);
Bean(param);
}
});
// 加载
VBox root = loader.load();
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args){
SpringbootJavafxDemoApplication.args = args;
launch(args);
}
@Override
public void init()throws Exception {
// 启动springboot
applicationContext = SpringApplication.run(SpringbootJavafxDemoApplication.class, args);
}
@Override
public void stop()throws Exception {
// 关闭springboot
applicationContext.stop();
}
}
/fxml/login.fxml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<?import l.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import Font?>
<VBox maxHeight="-Infinity"maxWidth="-Infinity"minHeight="-Infinity"minWidth="-Infinity"prefHeight="330.0"
controller="ller.LoginController"
prefWidth="430.0"xmlns="javafx/javafx/11"fx="javafx/fxml/1">
<AnchorPane prefHeight="330.0"prefWidth="430.0">
<children>
<Label layoutX="99.0"layoutY="183.0"text="账号:">
<font>
<Font size="14.0"/>
</font>
</Label>
<Label layoutX="99.0"layoutY="217.0"text="密码:">
<font>
<Font size="14.0"/>
</font>
</Label>
<Button id="loginButton"layoutX="97.0"layoutY="270.0"mnemonicParsing="false"prefHeight="35.0"prefWidth="236.0"
text="登录"textAlignment="CENTER"textFill="WHITE">
<font>
<Font size="14.0"/>
</font>
</Button>
<CheckBox layoutX="98.0"layoutY="243.0"mnemonicParsing="false"text="⾃动登录"textFill="#a6a6a6"/>
<CheckBox layoutX="188.0"layoutY="243.0"mnemonicParsing="false"text="记住密码"textFill="#a6a6a6"/>
<Label layoutX="277.0"layoutY="242.0"text="回密码"textFill="#a6a6a6">
<font>
<Font size="14.0"/>
</font>
</Label>
<TextField layoutX="146.0"layoutY="183.0"prefHeight="23.0"prefWidth="182.0"/>
<PasswordField layoutX="146.0"layoutY="216.0"prefHeight="23.0"prefWidth="182.0"/>
</children>
</AnchorPane>
</VBox>
编写⼀个controller触发按钮事件
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论