Spring Boot JavaFX starter

Spring Boot JavaFX starter

为了方便的将Spring boot 与 JavaFX结合,可以方便的在JavaFx中使用Spring boot的能力,自己写个包发布到中央仓库

Maven

添加以下依赖

<dependency>
    <groupId>io.github.podigua</groupId>
    <artifactId>javafx-podigua-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

用法

创建一个类,继承AbstractJavafxApplication,然后调用launch方法启动JavaFX程序

用法1

使用普通方法启动界面

@SpringBootApplication
public class TestApplication extends AbstractJavafxApplication {
    public static void main(String[] args) {
        launch(TestApplication.class, args);
    }

    @Override
    protected void ready(Stage stage) {
        VBox root = new VBox();
        Button button = new Button("测试");
        button.setOnAction(event -> {
            Alert alert = new Alert(AlertType.NONE, "点击了按钮", ButtonType.OK);
            alert.showAndWait();
        });
        root.getChildren().add(button);
        Scene scene = new Scene(root, 1000, 750);
        stage.setScene(scene);
        stage.show();
    }
}

用法2

使用Fxml启动界面

启动类

FxmlService 封装了加载fxml文件的方法,并获取spring容器中的对应的bean

需要在fxml文件中声明fx:controller


@SpringBootApplication
public class TestApplication extends AbstractJavafxApplication {
    public static void main(String[] args) {
        launch(TestApplication.class, args);
    }

    @Autowired
    private FxmlService fxmlService;

    @Override
    protected void ready(Stage stage) {
        Parent parent = fxmlService.load("fxml/index.fxml");
        stage.setScene(new Scene(parent, 1000, 750));
        stage.show();
    }
}

fxml

路径为:fxml/index.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="com.example.javafx.IndexController"
            prefHeight="400.0" prefWidth="600.0">
    <Button text="测试" onAction="#test"></Button>
</AnchorPane>

controller

controller 需要添加@Service使controller注册为springbean


@Service
public class IndexController implements Initializable {
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        System.out.println("initialize");
    }

    public void test(ActionEvent event) {
        Alert alert = new Alert(AlertType.NONE, "点击了按钮", ButtonType.OK);
        alert.showAndWait();
    }
}

动画

spring boot 加载过程较慢,spring加载过程中会展示一个动画,动画默认由DefaultSplashScreen提供,默认可见

创建一个类实现SplashScreen可以修改是否可见,以及图片的选择,若不以图片的方式展示,可以自行定义getParent方法

public class ImageSplashScreen implements SplashScreen {

    @Override
    public boolean visible() {
        return true;
    }

    @Override
    public String getImagePath() {
        return "start.png";
    }
}

启动类调用launch的重载方法

@SpringBootApplication
public class TestApplication extends AbstractJavafxApplication {
    public static void main(String[] args) {
        launch(TestApplication.class, new ImageSplashScreen(), args);
    }

    @Autowired
    private FxmlService fxmlService;

    @Override
    protected void ready(Stage stage) {
        Parent parent = fxmlService.load("fxml/index.fxml");
        stage.setScene(new Scene(parent, 1000, 750));
        stage.show();
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前言:现在脱离Spring-Boot已经不会写代码了。 之前写过一篇依赖springboot-javafx-sup...
    破地瓜阅读 6,854评论 0 3
  • 前言 对于正在构建的Spring Boot应用程序,我们不希望每次都从0开始实现某些特定功能。相反,我们希望仅实现...
    大哥你先走阅读 5,096评论 0 53
  • Spring Boot使用过程中,经常需要和很多注解打交道。也是我们常说的注解编程。所以接下来我们对Spri...
    tuacy阅读 5,535评论 0 11
  • 16宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...
    yichen大刀阅读 11,311评论 0 4
  • 公元:2019年11月28日19时42分农历:二零一九年 十一月 初三日 戌时干支:己亥乙亥己巳甲戌当月节气:立冬...
    石放阅读 11,820评论 0 2

友情链接更多精彩内容