gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:04:57 25 4
gpt4 key购买 nike

这是我第一次将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com