问题/目标摘要:
我想在VSCode中使用Scene Builder制作一个JavaFXGUI。一个简单的“hello world”程序在VSCode中运行没有问题。我还可以在VSCode中运行一个简单的“hello world”JavaFX代码而没有问题。但是,带有Scene Builder代码的JavaFX给我一个错误。
这可能是问题的一部分,但运行代码仅适用于非JavaFX“hello world”。运行JavaFX(非场景生成器)的唯一方法是通过“运行Java”。
版本:
相关扩展(没有链接;发布标记的垃圾邮件并试图绕过它):
详细说明(抱歉,这将是很多代码;想提供尽可能多的信息):
简单的Hello World:
public class testing
{
public static void main(String [] args)
{
System.out.println("HI");
}
}
此代码在运行代码和运行Java中运行,输出为HI。
JavaFX Hello World:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloWorld extends Application {
/**
* @param args
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
使用“运行代码”会出现20个错误,这里有两个(我的问题被标记为垃圾邮件,也许这会改变它……?):
HelloWorld.java:1: error: package javafx.application does not exist
import javafx.application.Application;
^
HelloWorld.java:2: error: package javafx.event does not exist
import javafx.event.ActionEvent;
20 errors
使用“运行Java”给出了预期的输出:一个带有按钮的窗口,上面写着“说‘Hello World’”,单击后打印“Hello World!”到控制台。图片:这里的图片
更复杂的场景构建器
代码来自这个YouTube视频。
这是. fxml文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="417.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MainSceneController">
<children>
<Label layoutX="14.0" layoutY="14.0" prefHeight="19.0" prefWidth="392.0" text="Title" />
<TextField fx:id="tfTitle" layoutX="14.0" layoutY="33.0" prefHeight="27.0" prefWidth="392.0" />
<Button layoutX="184.0" layoutY="359.0" mnemonicParsing="false" onAction="#btnOkClicked" text="OK" />
</children>
</AnchorPane>
这是启动. 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": "testing",
"request": "launch",
"mainClass": "testing",
"projectName": "Java VSCode test_a97e49e3"
},
{
"type": "java",
"name": "TestingBasic",
"request": "launch",
"mainClass": "TestingBasic",
"projectName": "Java VSCode test_a97e49e3"
},
{
"type": "java",
"name": "App",
"request": "launch",
"mainClass": "App",
"projectName": "Java VSCode test_a97e49e3"
},
{
"type": "java",
"name": "Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "HelloWorld",
"request": "launch",
"vmArgs": "--module-path C:/openjfx-19.0.2.1_windows-x64_bin-sdk/javafx-sdk-19.0.2.1/lib --add-modules javafx.controls,javafx.fxml",
"mainClass": "HelloWorld",
"projectName": "Java VSCode test_a97e49e3"
}
]
}
这是MainSceneController. fxml:
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class MainSceneController {
@FXML
private TextField tfTitle;
@FXML
void btnOkClicked(ActionEvent event) {
Stage mainWindow = (Stage) tfTitle.getScene().getWindow();
String title = tfTitle.getText();
mainWindow.setTitle(title);
}
}
以下是App.java:
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage primaryStage) {
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("MainScene.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
}
}
public static void main(String[] args) {
System.out.println("got here");
launch(args);
}
}
没有代码有错误。在中运行
会产生main
App.java
错误:缺少JavaFX运行时组件,并且需要运行此应用程序
。
App.java的运行Java给出了相同的结果;运行代码App.java给出了14个错误,类似于之前的20个关于java. fx包不存在的错误。)
我很困惑这意味着什么。
作为参考,这里是“为项目配置运行时”页面:这里。我有两个选项(17和19),但都失败并给我运行时错误:这里。
如果你能给我指出正确的方向,那就太好了。我是StackOverflow的新手,所以我感谢您的支持,如果我犯了错误,我道歉。
感谢@Slaw为我指明了正确的方向。
解决方案:这是一个愚蠢的错误。从我开始的配置来看,我只需要:
对于src文件夹:不知道为什么我需要它,但是把它们拿出来会引起问题。
对于vmArgs:表单"vmArgs":"--module-path\"C:/用户/Oliver/下载/javafx-sdk-19.0.2.1/lib\"--add-module javafx.控件,javafx. fxml",
中的"vmArgs":"--module-path\"部分必须用于每个使用JavaFX的"type"。
例如,这是我的启动. 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": "testing",
"request": "launch",
"vmArgs": "--module-path \"C:/Users/Oliver/Downloads/javafx-sdk-19.0.2.1/lib\" --add-modules javafx.controls,javafx.fxml",
"mainClass": "src.testing",
"projectName": "Java VSCode test_a97e49e3"
},
{
"type": "java",
"name": "testing",
"request": "launch",
"vmArgs": "--module-path \"C:/Users/Oliver/Downloads/javafx-sdk-19.0.2.1/lib\" --add-modules javafx.controls,javafx.fxml",
"mainClass": "testing",
"projectName": "Java VSCode test_a97e49e3"
},
{
"type": "java",
"name": "TestingBasic",
"request": "launch",
"vmArgs": "--module-path \"C:/Users/Oliver/Downloads/javafx-sdk-19.0.2.1/lib\" --add-modules javafx.controls,javafx.fxml",
"mainClass": "TestingBasic",
"projectName": "Java VSCode test_a97e49e3"
},
{
"type": "java",
"name": "App",
"request": "launch",
"vmArgs": "--module-path \"C:/Users/Oliver/Downloads/javafx-sdk-19.0.2.1/lib\" --add-modules javafx.controls,javafx.fxml",
"mainClass": "App",
"projectName": "Java VSCode test_a97e49e3"
},
{
"type": "java",
"name": "Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "HelloWorld",
"request": "launch",
"vmArgs": "--module-path \"C:/Users/Oliver/Downloads/javafx-sdk-19.0.2.1/lib\" --add-modules javafx.controls,javafx.fxml",
"mainClass": "HelloWorld",
"projectName": "Java VSCode test_a97e49e3"
}
]
}
对于我这种技术水平的人来说,请注意:vmArgs部分位于每个*部分;每个部分适用于您正在运行的每个文件(或java类)。
*技术上不
我猜当您启动JavaFX应用程序时会发生错误。
>
对于第一个问题Run Code
和RunJava
实际上是由不同的扩展提供的。前者是由Code Runner提供的,而后者是JavaDebugger扩展提供的。我建议始终使用RunJava
,因为代码运行器无法理解您项目的依赖关系,当您的文件使用第三方库时,您会收到错误。