Spring Boot JavaFX starter

Spring Boot JavaFX starter

為了方便的將Spring boot 與 JavaFX結(jié)合,可以方便的在JavaFx中使用Spring boot的能力,自己寫個(gè)包發(fā)布到中央倉庫

Maven

添加以下依賴

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

用法

創(chuàng)建一個(gè)類,繼承AbstractJavafxApplication,然后調(diào)用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, "點(diǎn)擊了按鈕", 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容器中的對應(yīng)的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, "點(diǎn)擊了按鈕", ButtonType.OK);
        alert.showAndWait();
    }
}

動畫

spring boot 加載過程較慢,spring加載過程中會展示一個(gè)動畫,動畫默認(rèn)由DefaultSplashScreen提供,默認(rèn)可見

創(chuàng)建一個(gè)類實(shí)現(xiàn)SplashScreen可以修改是否可見,以及圖片的選擇,若不以圖片的方式展示,可以自行定義getParent方法

public class ImageSplashScreen implements SplashScreen {

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

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

啟動類調(diào)用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();
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言:現(xiàn)在脫離Spring-Boot已經(jīng)不會寫代碼了。 之前寫過一篇依賴springboot-javafx-sup...
    破地瓜閱讀 2,443評論 0 3
  • 前言 對于正在構(gòu)建的Spring Boot應(yīng)用程序,我們不希望每次都從0開始實(shí)現(xiàn)某些特定功能。相反,我們希望僅實(shí)現(xiàn)...
    大哥你先走閱讀 1,357評論 0 53
  • Spring Boot使用過程中,經(jīng)常需要和很多注解打交道。也是我們常說的注解編程。所以接下來我們對Spri...
    tuacy閱讀 1,688評論 0 11
  • 16宿命:用概率思維提高你的勝算 以前的我是風(fēng)險(xiǎn)厭惡者,不喜歡去冒險(xiǎn),但是人生放棄了冒險(xiǎn),也就放棄了無數(shù)的可能。 ...
    yichen大刀閱讀 7,542評論 0 4
  • 公元:2019年11月28日19時(shí)42分農(nóng)歷:二零一九年 十一月 初三日 戌時(shí)干支:己亥乙亥己巳甲戌當(dāng)月節(jié)氣:立冬...
    石放閱讀 7,384評論 0 2

友情鏈接更多精彩內(nèi)容