我正在VSCode中处理一个JavaFX Maven项目,并试图将gson导入到我的一个类中,但它不允许我这样做.VSCode给了我一个"类型com.google.gson.gson是不可访问的"错误,并拒绝让我使用gson.
该依赖项已添加到我的pom.xml文件中,我在网上try 了一些解决方案,但都不起作用.同样奇怪的是,Gson与我同时处理的另一个Spring Maven项目工作得很好,所以我不太确定问题是什么.
下面是我的pom.xml文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ca.cmpt213.asn5</groupId>
<artifactId>client</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.6</version>
<executions>
<execution>
<!-- Default configuration for running -->
<!-- Usage: mvn clean javafx:run -->
<!-- <id>default-cli</id>
<configuration>
<mainClass>ca.cmpt213.asn5.</mainClass>
</configuration> -->
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
下面是我正在使用的类,它不能导入Gson:
package ca.cmpt213.asn5;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.google.gson.Gson; //gives a squiggly red line under this statement
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class SuperhumanTracker extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Label title = new Label("Superhuman Tracker");
title.setFont(new Font(20));
TableView table = new TableView<>();
TableColumn idColumn = new TableColumn<>("ID");
TableColumn nameColumn = new TableColumn<>("NAME");
TableColumn superpowerColumn = new TableColumn<>("SUPERPOWER");
TableColumn abilityScoreColumn = new TableColumn<>("ABILITY SCORE");
idColumn.setMinWidth(25);
nameColumn.setMinWidth(200);
superpowerColumn.setMinWidth(150);
abilityScoreColumn.setMinWidth(150);
table.getColumns().addAll(idColumn, nameColumn, superpowerColumn, abilityScoreColumn);
Button addButton = new Button("Add a Superhuman");
ComboBox<Integer> idComboBox = new ComboBox<>();
idComboBox.setPromptText("ID");
idComboBox.getItems().addAll(1, 2, 3);
Button viewButton = new Button("Get Details");
Button deleteButton = new Button("Delete Superhuman");
viewButton.setOnAction(event -> {
try {
long id = Long.valueOf(idComboBox.getValue());
URL url = new URL("http://localhost:8080/api/superhuman/" + id);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = reader.readLine();
Gson gson = new Gson(); //gives a squiggly red line under the Gson class
System.out.println(connection.getResponseCode());
connection.disconnect();
} catch (IOException ioException) {
ioException.printStackTrace();
}
Scene scene = new Scene(new SuperhumanDetails(null, 0, 0, null, null, null, null, 0).getView(), 400, 500);
Stage stage = new Stage();
stage.setTitle("Superhuman Detailed View");
stage.setScene(scene);
stage.show();
});
HBox hboxLeft = new HBox(idComboBox, separator1, viewButton, separator2, deleteButton);
hboxLeft.setAlignment(Pos.CENTER_LEFT);
HBox hboxRight = new HBox(addButton);
hboxRight.setAlignment(Pos.CENTER_RIGHT);
HBox hboxControls = new HBox(hboxLeft, hboxRight);
hboxControls.setSpacing(180);
VBox vbox = new VBox(title, table, hboxControls);
vbox.setAlignment(Pos.CENTER);
vbox.setSpacing(10);
vbox.setPadding(new Insets(10,10,10,10));
Scene scene = new Scene(vbox, 600, 400);
primaryStage.setTitle("Superhuman Tracker");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch();
}
}
以下是MODULE-INFO.Java文件:
module ca.cmpt213.asn5 {
requires transitive javafx.controls;
exports ca.cmpt213.asn5;
}