先看一下效果
看一下项目目录
码云源码:https://gitee.com/orzdh/springboot-javafx
1、Maven导入Springboot依赖包
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test
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, 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文件
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() {
@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("注册被点击");
}
}
这样就整合完成了。



