Javafx Spring Boot - 运行应用程序时出错

问题描述 投票:0回答:3

这是我第一次将 Javafx 与 Spring Boot 一起使用 运行我的应用程序时出现以下错误

Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.0.RELEASE:run (default-cli) on project basic: Could not exec java: Application finished with exit code: 1 -> [Help 1]

主班

@SpringBootApplication
public class BasicApplication extends Application {

private Parent root;
private ConfigurableApplicationContext context;

@Override
public void init() throws Exception {
    context = SpringApplication.run(BasicApplication.class);
    FXMLLoader loader=new FXMLLoader(getClass().getResource("/fxml/Scene.fxml"));
    loader.setControllerFactory(context::getBean);
    root=loader.load();
}

@Override
public void start(Stage primaryStage) throws Exception {
    Scene scene = new Scene(root);

    primaryStage.setScene(scene);
    primaryStage.show();
}

@Override
public void stop() throws Exception {
    context.close();
}

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

}

控制器类

public class SceneController implements Initializable {

@FXML
private Label label;

@FXML
private void buttonAction(){
    label.setText("Hello World!");
} 

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    

}

pom.xml

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

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.example</groupId>
<artifactId>basic</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>basic</name>
<description>Basic project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
            </configuration>
        </plugin>
    </plugins>
</build>

请问这是怎么回事。 为什么org.springframework.boot:spring-boot-maven-plugin:2.0.0.RELEASE:run无法执行?

场景.fxml代码

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="com.example.SceneController">
<children>
    <Button layoutX="126" layoutY="90" onAction="#buttonAction" text="Click Me!" />
    <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
</children>

正在使用 NetBeans IDE 运行应用程序

java spring javafx
3个回答
1
投票

好的已经知道为什么会发生错误了。我没有在控制器类中包含

@Component
。我的错误。

@Component
public class SceneController implements Initializable {
   @FXML
   private Label label;

   @FXML
   private void buttonAction(){
        label.setText("Hello World!");
   }

   @Override
   public void initialize(URL url, ResourceBundle rb) {
         // TODO
   }

}

`现在一切正常


0
投票

我注意到您没有显示 Scene.fxml 文件(指定应用程序布局的文件)或项目的目录结构。

检查 /src/main/resources/fxml/ 目录中是否有 Scene.fxml 文件。

我已经运行了您的代码,包括在我的机器中使用 https://start.spring.io/ 创建一个基础项目,并将其用作 Scene.fxml:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx">
    <Label text="Main Content"/>
</AnchorPane>

并运行命令 mvn clean spring-boot:run 并且它工作正常。

检查JAVA_HOME环境变量是否配置正确。

点击此链接了解 javafx 与 spring boot 的介绍: https://wimdeblauwe.wordpress.com/2017/09/18/using-spring-boot-with-javafx/

希望这有帮助。


0
投票

补充:这里有两个现成的模板,包含 JavaFX、Spring Boot 和 Maven/Gradle。

我将感谢 GitHub 上的明星支持它的免费开源,从而帮助您解决问题。 <3

https://github.com/davidweber411/javafx-JavaFxSpringBootGradleApp

https://github.com/davidweber411/javafx-JavaFxSpringBootMavenApp

© www.soinside.com 2019 - 2024. All rights reserved.