2022-08-11_springboot项目中使用javaFX

2022-08-10  本文已影响0人  微笑碧落

前言

1.IDEA中新建springboot项目

2.引用依赖

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

3.启动类的写法

@SpringBootApplication
public class DiskCleanerApplication extends AbstractJavaFxApplicationSupport {

    public static void main(String[] args) {
        launch(DiskCleanerApplication.class, MainView.class, args);
    }
}

4.创建view

@FXMLView(value = "/fxml/main.fxml")
public class MainView extends AbstractFxmlView {
}
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="com.glg.diskcleaner.controller.MainController"
            prefHeight="400.0" prefWidth="600.0">
    <children>
        <Button fx:id="button" layoutX="260" layoutY="180" text="But"></Button>
    </children>
</AnchorPane>

5.controller类

@FXMLController
public class MainController implements Initializable {
    private Stage primaryStage;
    @FXML // 可忽略
    public Button button;// 注意必须为public修饰符
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        primaryStage = DiskCleanerApplication.getStage();
        button.setOnAction(event -> {
            Alert alert = new Alert(Alert.AlertType.INFORMATION, "hello world");
            alert.initOwner(primaryStage);
            alert.showAndWait();
        });
    }
}

6.至此,完工。

引用

1.JavaFX之springboot搭建
2.使用springboot创建javafx的几种方式

上一篇 下一篇

猜你喜欢

热点阅读