首页 > 解决方案 > 不清楚如何设置 JavaFX (VSCode)

问题描述

我已经开始做一个 JavaFX 项目,并且我已经按照这个问题的设置说明进行操作:IntelliJ can't identify JavaFX 11 with OpenJDK 11 using VSCode。我在 Macbook 上。

我已经到了最后一部分,我将使用 settings.json 和 launch.json 进行设置。我尽可能完全按照说明进行操作,但错误:Error: JavaFX runtime components are missing, and are required to run this application不断出现。

这是我的 settings.json 和 launch.json 文件的样子:

设置.json

{
    "maven.view": "flat",
    "java.project.referencedLibraries": [
        "lib/**/*.jar",
        "javafx-sdk-11.0.2/**/*.jar"
    ],
    "java.dependency.packagePresentation": "hierarchical"
}

启动.json

{
    "configurations": [
        {
            "type": "java",
            "name": "CodeLens (Launch) - Main",
            "request": "launch",
            "mainClass": "src.Main",
            "projectName": "<censored>"
        },
        {
            "type": "java",
            "name": "CodeLens (Launch) - Main",
            "request": "launch",
            "vmArgs": "--module-path <path_from_root_directory_through_desktop_to_folder_with_project>/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml",
            "mainClass": "Main",
            "projectName": "Main"
        }
    ]
}

为了安全起见,我已将 JavaFX SDK 的实际路径替换为:<path_from_root_directory_through_desktop_to_folder_with_project>。

路径本身假设 VSCode 编译器从根目录开始以查找 JavaFX SDK(我已经尝试过,假设它在正在运行的项目中开始,但这仍然不起作用)。

我已将“mainClass”和“projectName”分配给包含我所有代码的 java 文件的文件名(减去 .java 扩展名)

这是我正在使用的java文件的代码,如果有帮助的话:

package src;

import javafx.scene.layout.Pane;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javafx.application.Application;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws FileNotFoundException {
        //declares a pane
        Pane pane = new Pane();
        
        //IMAGE ONE
        //creates a new imageView
        ImageView image1 = new ImageView(new Image(new FileInputStream("c1.gif")));
        
        //binds the image to the left of the center of the pane width
        image1.xProperty().bind((pane.widthProperty().divide(2)).subtract(36));
        
        //binds the image to the center of the pane height
        image1.yProperty().bind(pane.widthProperty().divide(2));
        
        //formats the image
        image1.setFitWidth(71);
        image1.setFitHeight(96);
        image1.setPreserveRatio(true);
        
        //adds the image
        pane.getChildren().add(image1);
        
        
        //IMAGE TWO
        //creates a new imageView
        ImageView image2 = new ImageView(new Image(new FileInputStream("c2.gif")));
        
        //binds the image to the center of the pane width
        image2.xProperty().bind(pane.widthProperty().divide(2));
        
        //binds the image to the center of the pane height
        image2.yProperty().bind(pane.widthProperty().divide(2));
        
        //formats the image
        image2.setFitWidth(71);
        image2.setFitHeight(96);
        image2.setPreserveRatio(true);
        
        //adds the image
        pane.getChildren().add(image2);
        
        
        //IMAGE THREE
        //creates a new imageView
        ImageView image3 = new ImageView(new Image(new FileInputStream("c3.gif")));
        
        //binds the image to the right of the center of the pane width
        image3.xProperty().bind((pane.widthProperty().divide(2)).add(36));
        
        //binds the image to the center of the pane height
        image3.yProperty().bind(pane.widthProperty().divide(2));
        
        //formats the image
        image3.setFitWidth(71);
        image3.setFitHeight(96);
        image3.setPreserveRatio(true);
        
        //adds the image
        pane.getChildren().add(image3);
        
        
        //SCENE SETUP
        //adds the pane with images to new scene
        Scene scene = new Scene(pane, 500, 500);
        
        //sets title of stage
        primaryStage.setTitle("DisplayImage");
        
        //displays the stage
        primaryStage.show();
        }
    
    public static void main(String []args) {
        launch(args);
        }
    }

请澄清我是否做错了什么。

只是为了澄清,我不是在问代码是否有任何问题,我想知道我在使用 JavaFX SDK 时做错了什么

可能没有足够的信息,请让我知道我需要提供哪些其他信息。

标签: javajavafxvisual-studio-code

解决方案


1.检查安装和环境配置。

参考:JavaFX 入门

2.检查设置java.configuration.runtimes,VS Code从中检测项目所需的运行时并选择适当的配置。

参考:项目的JDK

如果没有实现,请安装另一个 jdk 并尝试一下。


推荐阅读