【问题标题】:Unclear on how to setup JavaFX (VSCode)不清楚如何设置 JavaFX (VSCode)
【发布时间】:2021-01-29 10:07:46
【问题描述】:

我已经开始做一个 JavaFX 项目,并且我已经按照这个问题的设置说明进行操作:IntelliJ can't recognize 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 文件的样子:

settings.json

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

launch.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 的实际路径替换为:

路径本身假设 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 时做错了什么

信息可能不够,请告诉我需要提供的其他信息。

【问题讨论】:

  • 您确定您已正确安装 jfx sdk 吗?另请注意,jvm 参数的(更好的 IMO)替代方法是将 module-info.java 添加到定义了依赖项的项目中。
  • 为什么要参考有关 IntelliJ 的教程? stackoverflow.com/questions/54349894/javafx-11-with-vscode
  • @sprinter 我不确定我是否正确安装了它。
  • @Sedrick 这是我在网上搜索时得到的最接近的结果,它让我非常接近让它工作。

标签: java javafx visual-studio-code


【解决方案1】:

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

参考:Getting started with JavaFX

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

参考:JDK for projects

如果没有任何结果,请安装另一个 jdk 并尝试一下。

【讨论】:

    猜你喜欢
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 2020-06-21
    • 2012-12-06
    相关资源
    最近更新 更多