javafx+springboot学习笔记(如何无边框与换图标)

简述

众所周知,java写GUI属于一种自虐行为,无奈java课设需要,又不想用swing(),于是就采用了javafx+springboot的模式,关键是springboot真的有对javafx的支持

<dependency>
    <groupId>de.roskenet</groupId>
    <artifactId>springboot-javafx-support</artifactId>
    <version>2.1.6</version>
</dependency>

不过这玩意的学习文档不能用少来形容了(果然javafx是被放弃的技术),于是只能边看少量资料自读源码硬啃

顺带一提JavaFX之springboot-javafx-support解析-CSDN博客

讲的很好

启动类

@MapperScan("com.sounfury.sounfury_qx.mapper")
@SpringBootApplication
public class SounfuryQxApplication extends AbstractJavaFxApplicationSupport {


    public static void main(String[] args) {
      //启动
        launch(SounfuryQxApplication.class, LoginView.class,new SplashScreenCustom(), args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        //设置主题
        Application.setUserAgentStylesheet(PrimerLight.class.getResource("primer-light.css").toExternalForm());

        stage.getIcons().add(new Image("image/icon.png"));
        //不可拉伸
        stage.setResizable(false);
        super.start(stage);
    }

    @Override
    public void beforeInitialView(Stage stage, ConfigurableApplicationContext ctx) {
        stage.initStyle(StageStyle.TRANSPARENT);
        super.beforeInitialView(stage, ctx);
    }



}

只需要将SounfuryQxApplication 继承 AbstractJavaFxApplicationSupport即可

image-20240111121421200

这是正常的javafx启动流程,springboot-javafx-support对其进行了重写(除launch),这里着重讲一下init和 start

init

init源代码

public void init() throws Exception {
    this.defaultIcons.addAll(this.loadDefaultIcons());
    CompletableFuture.supplyAsync(() -> {
        return SpringApplication.run(this.getClass(), savedArgs);
    }).whenComplete((ctx, throwable) -> {
        if (throwable != null) {
            LOGGER.error("Failed to load spring application context: ", throwable);
            Platform.runLater(() -> {
                showErrorAlert(throwable);
            });
        } else {
            Platform.runLater(() -> {
                this.loadIcons(ctx);//换图标
                this.launchApplicationView(ctx);
            });
        }

    }).thenAcceptBothAsync(this.splashIsShowing, (ctx, closeSplash) -> {
        Platform.runLater(closeSplash);
    });
}
  1. 异步启动springboot主程序,将springboot应用上下文添加到springboot-javafx-support
  2. 加载应用图标
  3. springboot启动完成后启动javaFX
  4. 再开一线程异步加载闪屏线程
换图标

如果直接在start里面stage.getIcons().add(new Image("image/icon.png")); 会发现这是不可行的,其因是因为在init里面启动javafx时有这一步this.loadIcons(ctx);

进一步看源代码

private void loadIcons(ConfigurableApplicationContext ctx) {
    try {
        List<String> fsImages = PropertyReaderHelper.get(ctx.getEnvironment(), "javafx.appicons");
        if (!fsImages.isEmpty()) {
            fsImages.forEach((s) -> {
                Image img = new Image(this.getClass().getResource(s).toExternalForm());
                icons.add(img);
            });
        } else {
            icons.addAll(this.defaultIcons);
        }
    } catch (Exception var3) {
        LOGGER.error("Failed to load icons: ", var3);
    }

}

真正获取到图标的无非是这一句

List<String> fsImages = PropertyReaderHelper.get(ctx.getEnvironment(), "javafx.appicons");

这里传入两个参数,一个是 Environment 对象,一个是字符串propName

即从spring框架的环境配置中找到图标,自此,真相大白

只需要建一个配置类constant

public class Constant {
    public final static String KEY_APPICONS= "javafx.appicons";
}

然后在application.properties里面添加一句javafx.appicons=image/icon.png

然后在start里面stage.getIcons().add(new Image("image/icon.png"));即可

不过这是换闪屏后的图标,在最初一步有this.loadDefaultIcons());

源码如下

public Collection<Image> loadDefaultIcons() {
    return Arrays.asList(new Image(this.getClass().getResource("/icons/gear_16x16.png").toExternalForm()), new Image(this.getClass().getResource("/icons/gear_24x24.png").toExternalForm()), new Image(this.getClass().getResource("/icons/gear_36x36.png").toExternalForm()), new Image(this.getClass().getResource("/icons/gear_42x42.png").toExternalForm()), new Image(this.getClass().getResource("/icons/gear_64x64.png").toExternalForm()));
}

这里设置好了初始图标,

所以直接重写方法就行

@Override
public Collection<Image> loadDefaultIcons() {
    return Arrays.asList(new Image(Objects.requireNonNull(this.getClass().getResource("/image/icon.png")).toExternalForm()));
}

这样闪屏前的图标也能换了

start

按例先上源码

public void start(Stage stage) throws Exception {
    GUIState.setStage(stage);
    GUIState.setHostServices(this.getHostServices());
    Stage splashStage = new Stage(StageStyle.TRANSPARENT);
    if (splashScreen.visible()) {
        Scene splashScene = new Scene(splashScreen.getParent(), Color.TRANSPARENT);
        splashStage.setScene(splashScene);
        splashStage.getIcons().addAll(this.defaultIcons);
        splashStage.initStyle(StageStyle.TRANSPARENT);
        this.beforeShowingSplash(splashStage);
        splashStage.show();
    }

    this.splashIsShowing.complete(() -> {
        this.showInitialView();//这一步
        if (splashScreen.visible()) {
            splashStage.hide();
            splashStage.setScene((Scene)null);
        }

    });
}
设置无边框窗口以及其他窗口

在start里面直接stage.initStyle(StageStyle.TRANSPARENT);发现不管用

所以直接看源码

可以看出在闪屏结束之后执行了showInitialView()

private void showInitialView() {
    String stageStyle = applicationContext.getEnvironment().getProperty("javafx.stage.style");
    if (stageStyle != null) {
        GUIState.getStage().initStyle(StageStyle.valueOf(stageStyle.toUpperCase()));
    } else {
        GUIState.getStage().initStyle(StageStyle.DECORATED);
    }

    this.beforeInitialView(GUIState.getStage(), applicationContext);
    showView(savedInitialView);
}

然后在这里面又在 showView前执行了beforeInitialView(GUIState.getStage(), applicationContext)

故直接重写方法即可

@Override
public void beforeInitialView(Stage stage, ConfigurableApplicationContext ctx) {
    stage.initStyle(StageStyle.TRANSPARENT);
    super.beforeInitialView(stage, ctx);
}

这样在showview前即可更改窗口了

顺便展示showview源代码

public static void showView(Class<? extends AbstractFxmlView> window, Modality mode) {
    AbstractFxmlView view = (AbstractFxmlView)applicationContext.getBean(window);
    Stage newStage = new Stage();
    Scene newScene;
    if (view.getView().getScene() != null) {
        newScene = view.getView().getScene();
    } else {
        newScene = new Scene(view.getView());
    }

    newStage.setScene(newScene);
    newStage.initModality(mode);
    newStage.initOwner(getStage());
    newStage.setTitle(view.getDefaultTitle());
    newStage.initStyle(view.getDefaultStyle());
    newStage.showAndWait();
}

newStage.initStyle(view.getDefaultStyle());

可以看出来会继承原来窗口(所以为什么不能直接传

故想要不同窗口间不同样式(不同图标),只能用传统方式,令人唏嘘

//先注入
 @Autowired
    private ApplicationContext applicationContext;

FXMLLoader fxmlLoader = new FXMLLoader(SounfuryQxApplication.class.getResource("/fxml/Main.fxml"));
fxmlLoader.setControllerFactory(applicationContext::getBean);//让spring管理
Scene newScene = new Scene(fxmlLoader.load());
newScene.setFill(null);
Stage newStage = new Stage();
newStage.setScene(newScene);
newStage.initStyle(StageStyle.DECORATED);
newStage.getIcons().add(new Image("image/icon.png"));
newStage.show();
Kotlin是一种静态类型的编程语言,具有JVM的可移植性和Java的互操作性。Spring Boot是一个用于创建独立的、基于Spring框架的Java应用程序的框架,它提供了快速开发应用程序所需的所有功能。JavaFX是一个用于创建丰富客户端应用程序的框架,它提供了丰富的UI组件和布局管理器。 要使用Kotlin Spring BootJavaFX开发桌面应用程序,需要完成以下步骤: 1. 创建一个Kotlin Spring Boot项目。可以使用Spring Initializr创建项目,选择Kotlin和Spring Web依赖项。 2. 添加JavaFX依赖项。可以在pom.xml文件中添加以下依赖项: ``` <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>16</version> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-fxml</artifactId> <version>16</version> </dependency> ``` 3. 创建一个JavaFX应用程序类。可以使用JavaFX的Application类作为应用程序的入口点。在这个类中,可以创建UI组件,处理事件和管理应用程序的状态。以下是一个简单的JavaFX应用程序类的示例: ```kotlin import javafx.application.Application import javafx.fxml.FXMLLoader import javafx.scene.Parent import javafx.scene.Scene import javafx.stage.Stage class MyApplication : Application() { override fun start(primaryStage: Stage?) { val root: Parent = FXMLLoader.load(javaClass.getResource("/fxml/main.fxml")) primaryStage?.title = "My Application" primaryStage?.scene = Scene(root) primaryStage?.show() } companion object { @JvmStatic fun main(args: Array<String>) { launch(MyApplication::class.java, *args) } } } ``` 4. 创建FXML布局文件。FXML是一种XML格式的文件,用于定义UI组件和布局。可以使用Scene Builder或手动创建FXML文件。以下是一个简单的FXML布局文件的示例: ```xml <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.layout.AnchorPane?> <AnchorPane xmlns:fx="http://javafx.com/fxml/1" fx:id="root" prefHeight="400" prefWidth="600"> <Button fx:id="button" text="Click me" layoutX="250" layoutY="180" /> </AnchorPane> ``` 5. 在JavaFX应用程序类中加载FXML布局文件。可以使用FXMLLoader类加载FXML布局文件,并将其添加到应用程序的场景图中。以下是一个示例: ```kotlin val root: Parent = FXMLLoader.load(javaClass.getResource("/fxml/main.fxml")) primaryStage?.title = "My Application" primaryStage?.scene = Scene(root) primaryStage?.show() ``` 6. 处理UI事件。可以在JavaFX应用程序类中添加事件处理程序,以响应UI组件的事件。以下是一个处理按钮单击事件的示例: ```kotlin button.setOnAction { event -> println("Button clicked!") } ``` 7. 使用Spring Boot管理应用程序的状态。可以使用Spring Boot的依赖注入和管理功能来管理应用程序的状态和依赖关系。可以在Spring Boot的配置类中定义bean,然后在JavaFX应用程序类中使用它们。以下是一个简单的Spring Boot配置类的示例: ```kotlin @Configuration class AppConfig { @Bean fun myService(): MyService { return MyService() } } ``` 8. 在JavaFX应用程序类中使用Spring Boot的依赖注入功能。可以在JavaFX应用程序类的构造函数中注入Spring Boot管理的bean。以下是一个示例: ```kotlin class MyApplication : Application() { @Autowired lateinit var myService: MyService // ... } ``` 这就是使用Kotlin Spring BootJavaFX开发桌面应用程序的基本步骤。当然,还有很多其他的细节和技术,可以根据需要进行学习和应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值