(C) the creation of javaFX + springboot maven based project creation

Created based javaFx + springboot maven project in two ways, first one is designed springboot UI integration by non-coding method; as a separate second user interface (UI) logic and backend integration springboot, wherein the user interface is fxml file.

maven dependence

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>de.roskenet</groupId>
            <artifactId>springboot-javafx-support</artifactId>
            <version>${springboot-javafx-support.version}</version>
        </dependency>    

Creating StartMain class, and inherit Application

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;

import java.util.Objects;
import java.util.function.Consumer;

/**
 * Maven project to start building JavaFX class
 */
public class StartMain extends Application implements CommandLineRunner, Consumer<Stage> {

    /**
     * Start window interface principle:
     In * 1. run (String ... args) assigned to springStartMain
     * 2. start (Stage primaryStage) is called in to operate primaryStage springStartMain
     * 3. springStartMain while an object is actually StartMain spring management process may therefore accept any object as management spring
     */
    private static Consumer<Stage> springStartMain;
    private final Button btnStart = new Button("开 始");

    @Override
    public void accept(Stage stage) {

        final GridPane gridPane = new GridPane();
        gridPane.setPrefWidth(700);
        gridPane.setPadding(new Insets(10, 10, 10, 10));
        gridPane.setVgap(10);
        gridPane.setHgap(10);
        btnStart.setStyle("-fx-padding: 10;-fx-end-margin:20;");
        HBox hBox1 = new HBox();
        hBox1.getChildren().addAll(btnStart);
        hBox1.setSpacing(20);
        gridPane.addRow(2, new Label(), hBox1);
        //页面
        Group root = new Group();
        Scene scene = new Scene(root, 720, 500);
        scene.setRoot(gridPane);

        // set the title 
        stage.setTitle ( " the Hello World " );
         // Stage's title will be the Hello 
        stage.setScene (SCENE);
        stage.show();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        springStartMain.accept(primaryStage);
    }

    @Override
    public void run(String... args) throws Exception {
        springStartMain = Objects.requireNonNull(this);
    }

    public static void main(String[] args) {
        //启动spring-boot
        SpringApplication.run(StartMain.class, args);
        //启动窗口
        Application.launch(args);
    }

}

Project is structured as follows:

 

Guess you like

Origin www.cnblogs.com/hww-2429/p/11750915.html