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

标签 java spring javafx

这是我第一次将 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);
}

}

Controller 类

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无法执行?

Scene.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 运行应用程序

最佳答案

好的,已经知道为什么会发生错误。我没有在 Controller 类中包含 @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
   }

}

`现在一切正常

关于Javafx Spring Boot - 运行应用程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51526374/

相关文章:

JavaFX:如何用具有相同父类(super class)的 2 个不同类填充 TableView

java - eclipse 中的 Junit 在使用 junit 时产生 NoClassDefFoundError

java - 参数化集合的Spring Autowiring

java - Whitelabel错误页面部署到服务器后出现错误

Java JPA 过滤器

3d - 3D 国际象棋场上的 JavaFX 独立透视图

java - 从 JavaScript 调用 Java 时出现不可恢复的 stackoverflow 错误

java - 如何实现 SAML SSO

java - Java 中等效的 findAll python 方法

spring - 如何在Spring EL中评估Spring EL