英文:
Error: JavaFX runtime components are missing, and are required to run this application. run the project with gradle
问题
这是我的Main.java文件:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import static java.lang.Integer.parseInt;
public class Duke extends Application {
private Storage storage;
private TaskList tasks;
private Ui ui;
private ScrollPane scrollPane;
private VBox dialogContainer;
private TextField userInput;
private Button sendButton;
private Scene scene;
private Image user = new Image(this.getClass().getResourceAsStream("images/DaUser.png"));
private Image duke = new Image(this.getClass().getResourceAsStream("images/DaDuke.png"));
@Override
public void start(Stage stage) {
// 步骤1. 设置所需的组件
// 聊天内容滚动的容器。
scrollPane = new ScrollPane();
dialogContainer = new VBox();
scrollPane.setContent(dialogContainer);
userInput = new TextField();
sendButton = new Button("Send");
AnchorPane mainLayout = new AnchorPane();
mainLayout.getChildren().addAll(scrollPane, userInput, sendButton);
scene = new Scene(mainLayout);
stage.setScene(scene);
stage.show();
stage.setTitle("Duke");
stage.setResizable(false);
stage.setMinHeight(600.0);
stage.setMinWidth(400.0);
mainLayout.setPrefSize(400.0, 600.0);
scrollPane.setPrefSize(385, 535);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
scrollPane.setVvalue(1.0);
scrollPane.setFitToWidth(true);
dialogContainer.setPrefHeight(Region.USE_COMPUTED_SIZE);
userInput.setPrefWidth(325.0);
sendButton.setPrefWidth(55.0);
AnchorPane.setTopAnchor(scrollPane, 1.0);
AnchorPane.setBottomAnchor(sendButton, 1.0);
AnchorPane.setRightAnchor(sendButton, 1.0);
AnchorPane.setLeftAnchor(userInput, 1.0);
AnchorPane.setBottomAnchor(userInput, 1.0);
sendButton.setOnMouseClicked((event) -> {
handleUserInput();
});
userInput.setOnAction((event) -> {
handleUserInput();
});
dialogContainer.heightProperty().addListener((observable) -> scrollPane.setVvalue(1.0));
}
private void handleUserInput() {
Label userText = new Label(userInput.getText());
Label dukeText = new Label(getResponse(userInput.getText()));
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(userText.getText(), user),
DialogBox.getDukeDialog(dukeText.getText(), duke)
);
userInput.clear();
}
public String getResponse(String input) {
return "Duke heard: " + input;
}
public Duke(String filePath) {
ui = new Ui();
storage = new Storage(filePath);
try {
tasks = new TaskList(storage.load());
} catch (DukeException e) {
ui.showLoadingError();
tasks = new TaskList();
}
}
public Duke() {
this("data/duke.txt");
}
public static void main(String[] args) {
new Duke("data/duke.txt").run();
}
public void run() {
ui.showWelcome();
boolean isExit = false;
while (!isExit) {
try {
String fullCommand = ui.readCommand();
ui.showLine();
Command c = Parser.parse(fullCommand);
c.execute(tasks, ui, storage);
isExit = c.isExit();
} catch (DukeException e) {
ui.showError(e.getMessage());
} finally {
ui.showLine();
}
}
}
}
这是你提到的另一个错误:
"我将lib文件夹导入项目,现在错误消失了。但是当我运行项目时出现了另一个错误,错误信息是:“Caused by: java.lang.NullPointerException: Input stream must not be null”。"
如果你有其他问题或需要更多帮助,请随时告诉我。
英文:
I am building a java project in intellij. I alreadly imported the gradle script and build.gradle file can be successfully built. However, when I ran my java files, this error occurs: "JavaFX runtime components are missing, and are required to run this application". I am really not sure what is the issue.
This is my Main.java file:
and this is the Duke.java files:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import static java.lang.Integer.parseInt;
public class Duke extends Application {
private Storage storage;
private TaskList tasks;
private Ui ui;
private ScrollPane scrollPane;
private VBox dialogContainer;
private TextField userInput;
private Button sendButton;
private Scene scene;
private Image user = new Image(this.getClass().getResourceAsStream("images/DaUser.png"));
private Image duke = new Image(this.getClass().getResourceAsStream("images/DaDuke.png"));
@Override
public void start(Stage stage) {
//Step 1. Setting up required components
//The container for the content of the chat to scroll.
scrollPane = new ScrollPane();
dialogContainer = new VBox();
scrollPane.setContent(dialogContainer);
userInput = new TextField();
sendButton = new Button("Send");
AnchorPane mainLayout = new AnchorPane();
mainLayout.getChildren().addAll(scrollPane, userInput, sendButton);
scene = new Scene(mainLayout);
stage.setScene(scene);
stage.show();
stage.setTitle("Duke");
stage.setResizable(false);
stage.setMinHeight(600.0);
stage.setMinWidth(400.0);
mainLayout.setPrefSize(400.0, 600.0);
scrollPane.setPrefSize(385, 535);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
scrollPane.setVvalue(1.0);
scrollPane.setFitToWidth(true);
// You will need to import `javafx.scene.layout.Region` for this.
dialogContainer.setPrefHeight(Region.USE_COMPUTED_SIZE);
userInput.setPrefWidth(325.0);
sendButton.setPrefWidth(55.0);
AnchorPane.setTopAnchor(scrollPane, 1.0);
AnchorPane.setBottomAnchor(sendButton, 1.0);
AnchorPane.setRightAnchor(sendButton, 1.0);
AnchorPane.setLeftAnchor(userInput, 1.0);
AnchorPane.setBottomAnchor(userInput, 1.0);
sendButton.setOnMouseClicked((event) -> {
handleUserInput();
});
userInput.setOnAction((event) -> {
handleUserInput();
});
dialogContainer.heightProperty().addListener((observable) -> scrollPane.setVvalue(1.0));
}
private void handleUserInput() {
Label userText = new Label(userInput.getText());
Label dukeText = new Label(getResponse(userInput.getText()));
dialogContainer.getChildren().addAll(
DialogBox.getUserDialog(userText.getText(), user),
DialogBox.getDukeDialog(dukeText.getText(), duke)
);
userInput.clear();
}
public String getResponse(String input) {
return "Duke heard: " + input;
}
public Duke(String filePath) {
ui = new Ui();
storage = new Storage(filePath);
try {
tasks = new TaskList(storage.load());
} catch (DukeException e) {
ui.showLoadingError();
tasks = new TaskList();
}
}
public Duke() {
this("data/duke.txt");
}
public static void main(String[] args) {
new Duke("data/duke.txt").run();
}
public void run() {
ui.showWelcome();
boolean isExit = false;
while (!isExit) {
try {
String fullCommand = ui.readCommand();
ui.showLine();
Command c = Parser.parse(fullCommand);
c.execute(tasks, ui, storage);
isExit = c.isExit();
} catch (DukeException e) {
ui.showError(e.getMessage());
} finally {
ui.showLine();
}
}
}
}
update: I imported the lib folder into my project and now the error is gone. But another error occurs when I ran the project, which is "Caused by: java.lang.NullPointerException: Input stream must not be null"
答案1
得分: 1
根据您的评论,您正在使用Java 13而不是Java 11。
JavaFX在Java 13中不是JDK的一部分,同样也不在Java 11中。您可以从openjfx.io获取JavaFX。然后,您需要将JavaFX的JAR文件添加到您的模块路径,因为自从JDK 9以来,Java使用模块。
与您的问题无关,但是"system"属性包含有关Java版本的详细信息,例如...
System.getProperty("java.runtime.version");
英文:
According to your comment you are using Java 13 and not Java 11.
JavaFX is not part of the JDK in Java 13 and also not in Java 11. You can get JavaFX from openjfx.io. Then you need to add the JavaFX JAR files to your module path because since JDK 9, java uses modules.
Unrelated to your question, but the "system" properties contain details about the java version, for example...
System.getProperty("java.runtime.version");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论