java - 找不到 FXML 路径

标签 java xml javafx

我对这个主题做了一些研究,但仍然卡住了。我还在堆栈溢出中搜索了其他执行此操作的方法,但到目前为止还没有运气。我尝试过没有示例,将其移动到另一个文件夹,但没有任何效果。结构如下:

enter image description here

主要Java代码

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root=FXMLLoader.load(getClass().getResource("sample/ui.fxml"));
    primaryStage.setScene(new Scene(root));
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}
}

Controller.java代码

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.text.Text;

public class Controller {

@FXML
private Text output;

@FXML
private void processNumpad(ActionEvent event){

}
@FXML
private void processOperator(ActionEvent event){

}

}

ui.fxml代码

    <?xml version="1.0" encoding="UTF-8"?>

<?import java.net.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.beans.property.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.paint.Color?>

<VBox spacing="10" alignment="CENTER" prefWidth="300" prefHeight="300" fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml">
    <fx:define>
        <Font fx:id="FONT" size="18" />
    </fx:define>

    <StackPane alignment="CENTER">
        <Rectangle fill="TRANSPARENT" stroke="GRAY" width="230" height="50" />
        <Text fx:id="output" font="$FONT" />
    </StackPane>

    <HBox spacing="10" alignment="CENTER">
        <Button text="7" prefWidth="50" font="$FONT" onAction="#processNumpad" />
        <Button text="8" prefWidth="50" font="$FONT" onAction="#processNumpad" />
        <Button text="9" prefWidth="50" font="$FONT" onAction="#processNumpad" />
        <Button text="/" prefWidth="50" font="$FONT" onAction="#processOperator" />
    </HBox>

    <HBox spacing="10" alignment="CENTER">
        <Button text="4" prefWidth="50" font="$FONT" onAction="#processNumpad" />
        <Button text="5" prefWidth="50" font="$FONT" onAction="#processNumpad" />
        <Button text="6" prefWidth="50" font="$FONT" onAction="#processNumpad" />
        <Button text="*" prefWidth="50" font="$FONT" onAction="#processOperator" />
    </HBox>

    <HBox spacing="10" alignment="CENTER">
        <Button text="1" prefWidth="50" font="$FONT" onAction="#processNumpad" />
        <Button text="2" prefWidth="50" font="$FONT" onAction="#processNumpad" />
        <Button text="3" prefWidth="50" font="$FONT" onAction="#processNumpad" />
        <Button text="-" prefWidth="50" font="$FONT" onAction="#processOperator" />
    </HBox>

    <HBox spacing="10" alignment="CENTER">
        <Button text="0" prefWidth="110" font="$FONT" onAction="#processNumpad" />
        <Button text="=" prefWidth="50" font="$FONT" onAction="#processOperator" />
        <Button text="+" prefWidth="50" font="$FONT" onAction="#processOperator" />
    </HBox>

</VBox>

错误

> Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at sample.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more

我正在关注this tutorial了解有关 JavaFX 和 xml 的更多信息。

最佳答案

这非常接近于以下内容的重复:

对于您的特定目录结构,以下内容将起作用:

getClass().getResource("ui.fxml")

上面假设您的 fxml 文件实际上称为 ui.fxml,正如您在问题中以粗体命名的那样(请注意,您的屏幕截图中有 example.fxml,所以那里出了问题...)。

请注意,getResource 调用不需要资源文件 ui.fxml 前面的 sample/,因为资源查找是相对于类位置(已位于示例文件夹中)的。如果您想要绝对查找而不是相对查找,可以使用 /sample/ui.fxml,这也可以。

calc

关于java - 找不到 FXML 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40922663/

相关文章:

java - 尝试读取 XML 文件后发生 UnknownHostException

java - 在 JavaFX TextField 中输入文本会卡住应用程序

java - 在自然语言中断处拆分字符串

java - Spock + Spring Boot Web - 获取异常消息

java - 在 DialogFragment 中使用 FragmentManager 和 FragmentTransaction

java - 如何检查一个字符串的字符与另一个字符串的其他字符不匹配

python - BeautifulSoup 美化自定义换行选项

xml - 为什么 MSXML 中的 createProcessingInstruction 会生成不完整的输出?

JavaFX改变TableView选择行栏的蓝色

java - 将全局按键监听器附加到多个按钮