我的 JavaFX 项目在编辑器中运行良好,但是当我尝试构建到 .jar 时它不会运行。我正在使用 java 17,所以我需要手动导入它,我已经在 settings.json 中正确导入了库,并在 launch.json 中添加了 vmArgs。当我运行java gui.java
- (gui.java 是我的类名)时,我收到错误消息,例如:error: package javafx.application does not exist
, error: package javafx.geometry does not exist
, error: package javafx.scene does not exist
. 我知道我应该使用不同版本的 java 或使用其他 vs code 的东西,但我不知道如何将项目转换为 java 的早期版本,我想使用 vs code。有谁知道我如何获得 .jar 或任何为其工作的可执行文件。我什至可以使用手动编译的 bash 脚本之类的东西,我只需要在编辑器之外运行它。这是我的 gui 代码,我知道它的分配很糟糕。
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Scanner;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class gui extends Application {
ArrayList<CheckBox> checkBoxes = new ArrayList<CheckBox>();
static String currentDir = System.getProperty("user.dir");
static File checkListFile = new File(currentDir + "\\list.TXT");
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
Scene scene = new Scene(root, 450, 250);
VBox vbCenter = new VBox();
TextField input = new TextField();
input.setOnKeyPressed( event -> {
if( event.getCode() == KeyCode.ENTER ) {
addCheckBox(input.getText(), vbCenter);
input.setText("");
saveCheckBoxes();
}
});
vbCenter.getChildren().add(input);
HBox hbButtons = new HBox();
Button reset = new Button("Reset");
reset.setOnAction( event -> {
removeCheckBoxes(vbCenter);
});
hbButtons.getChildren().add(reset);
hbButtons.setAlignment(Pos.CENTER_LEFT);
loadCheckBoxes(vbCenter);
root.setPadding(new Insets(20));
root.setCenter(vbCenter);
root.setBottom(hbButtons);
primaryStage.setTitle("Checklist");
primaryStage.setScene(scene);
primaryStage.show();
}
public void addCheckBox(String name, VBox vbCenter) {
CheckBox checkBox = new CheckBox(name);
checkBoxes.add(checkBox);
vbCenter.getChildren().add(checkBox);
}
public void removeCheckBoxes(VBox vbCenter) {
for (CheckBox checkBox : checkBoxes) {
vbCenter.getChildren().remove(checkBox);
}
checkBoxes = new ArrayList<CheckBox>();
try {
clearTheFile(checkListFile);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void clearTheFile(File file) throws IOException {
FileWriter fwOb = new FileWriter(file);
PrintWriter pwOb = new PrintWriter(fwOb, false);
pwOb.flush();
pwOb.close();
fwOb.close();
}
public void saveCheckBoxes() {
String[] name = new String[checkBoxes.size()];
for (int i = 0; i < checkBoxes.size(); i++) {
name[i] = checkBoxes.get(i).getText();
}
writeData(name, checkListFile);
}
public void loadCheckBoxes(VBox vb) {
String[] data = readData(checkListFile);
for (String s : data) {
CheckBox checkBox = new CheckBox(s);
checkBoxes.add(checkBox);
vb.getChildren().add(checkBox);
}
}
public String[] readData(File file) {
String[] result = new String[0];
try {
result = new String[(int)Files.lines(file.toPath()).count()];
Scanner scanner = new Scanner(file);
int index = 0;
while (scanner.hasNextLine()) {
result[index] = scanner.nextLine();
index++;
}
scanner.close();
}
catch (Exception e) {
e.printStackTrace();
}
return result;
}
public void writeData(String data, File file) {
try {
FileWriter writer = new FileWriter(file);
writer.write(data);
writer.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void writeData(String[] dataArr, File file) {
String data = "";
for (int i = 0; i < dataArr.length; i++) {
data += (dataArr[i] + "\n");
}
writeData(data, file);
}
}
启动.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Launch Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "Launch gui",
"request": "launch",
"vmArgs": "--module-path C:/Users/Billy1301/Downloads/openjfx-17.0.1_windows-x64_bin-sdk/javafx-sdk-17.0.1/lib --add-modules javafx.controls,javafx.fxml",
"mainClass": "gui",
"projectName": "Productivity_5753000c"
}
]
}
设置.json
{
"java.project.referencedLibraries": [
"lib/**/*.jar",
"c:\\Users\\Billy1301\\Downloads\\openjfx-17.0.1_windows-x64_bin-sdk\\javafx-sdk-17.0.1\\lib\\javafx.base.jar",
"c:\\Users\\Billy1301\\Downloads\\openjfx-17.0.1_windows-x64_bin-sdk\\javafx-sdk-17.0.1\\lib\\javafx.controls.jar",
"c:\\Users\\Billy1301\\Downloads\\openjfx-17.0.1_windows-x64_bin-sdk\\javafx-sdk-17.0.1\\lib\\javafx.fxml.jar",
"c:\\Users\\Billy1301\\Downloads\\openjfx-17.0.1_windows-x64_bin-sdk\\javafx-sdk-17.0.1\\lib\\javafx.graphics.jar",
"c:\\Users\\Billy1301\\Downloads\\openjfx-17.0.1_windows-x64_bin-sdk\\javafx-sdk-17.0.1\\lib\\javafx.media.jar",
"c:\\Users\\Billy1301\\Downloads\\openjfx-17.0.1_windows-x64_bin-sdk\\javafx-sdk-17.0.1\\lib\\javafx.swing.jar",
"c:\\Users\\Billy1301\\Downloads\\openjfx-17.0.1_windows-x64_bin-sdk\\javafx-sdk-17.0.1\\lib\\javafx.web.jar",
"c:\\Users\\Billy1301\\Downloads\\openjfx-17.0.1_windows-x64_bin-sdk\\javafx-sdk-17.0.1\\lib\\javafx-swt.jar"
]
}