JavaFX | javafx.fxml.LoadException: 缺少资源密钥

JavaFX | javafx.fxml.LoadException: Missing resource key

我是 JavaFX 的新手,我正在做一个简单的计算器项目。

我真的很困惑我的代码出了什么问题。 我猜 MainController.java 或 main.fxml 中有错误。 当我 运行 gradle 构建项目时,我得到 javafx.fxml.LoadException: Parent root = FXMLLoader.load(getClass().getResource("main.fxml")); 上缺少资源键我已经用 GitHub 中的其他示例项目检查了我的代码并尝试修复了错误但失败了。

有人知道为什么吗?

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: javafx.fxml.LoadException: Missing resource key.
/Users/allen/IdeaProjects/Calculator/build/resources/main/allen/edu/com/main.fxml:35

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.resolvePrefixedValue(FXMLLoader.java:420)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:370)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:332)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:242)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:775)
    at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2838)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2557)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
    at allen.edu.com.App.start(App.java:17)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Exception running application allen.edu.com.App

FAILURE: Build failed with an exception.

App.java

package allen.edu.com;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class App extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
        Scene scene = new Scene(root);
//        scene.getStylesheets().add("org/kordamp/bootstrapfx/bootstrapfx.css");
//        scene.getStylesheets().addAll(this.getClass().getResource("styles.css").toExternalForm());
        stage.setTitle("Calculator");
        stage.setResizable(false);
        stage.setScene(scene);
        stage.show();
    }

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

}

main.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>


<AnchorPane prefHeight="327.0" prefWidth="230.0" xmlns="http://javafx.com/javafx/10.0.2-internal"
            xmlns:fx="http://javafx.com/fxml/1" fx:controller="allen.edu.com.MainController">
    <VBox layoutX="-2.0" prefHeight="327.0" prefWidth="243.0">
        <Label fx:id="output"  alignment="CENTER_RIGHT" prefHeight="81.0" prefWidth="243.0"
               textAlignment="RIGHT">
            <font>
                <Font size="64.0"/>
            </font>
        </Label>
        <GridPane prefHeight="182.0" prefWidth="243.0">
            <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
            </columnConstraints>
            <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
            </rowConstraints>
            <Button fx:id="AC" prefHeight="53.0" prefWidth="60.0" text="AC" onAction="#handleButtonAction"/>
            <Button fx:id="invert" prefHeight="55.0" prefWidth="63.0" text="+/-" GridPane.columnIndex="1"
                    onAction="#handleButtonAction"/>
            <Button fx:id="percent" prefHeight="55.0" prefWidth="65.0" text="%" GridPane.columnIndex="2"
                    onAction="#handleButtonAction"/>
            <Button fx:id="divide" prefHeight="67.0" prefWidth="64.0" text="/" GridPane.columnIndex="3"
                    onAction="#handleButtonAction"/>
            <Button fx:id="seven" prefHeight="71.0" prefWidth="75.0" text="7" GridPane.rowIndex="1"
                    onAction="#handleButtonAction"/>
            <Button fx:id="eight" prefHeight="55.0" prefWidth="68.0" text="8" GridPane.columnIndex="1"
                    GridPane.rowIndex="1" onAction="#handleButtonAction"/>
            <Button fx:id="nine" prefHeight="74.0" prefWidth="69.0" text="9" GridPane.columnIndex="2"
                    GridPane.rowIndex="1" onAction="#handleButtonAction"/>
            <Button fx:id="multiply" prefHeight="64.0" prefWidth="65.0" text="x" GridPane.columnIndex="3"
                    GridPane.rowIndex="1" onAction="#handleButtonAction"/>
            <Button fx:id="four" prefHeight="53.0" prefWidth="70.0" text="4" GridPane.rowIndex="2"
                    onAction="#handleButtonAction"/>
            <Button fx:id="five" prefHeight="66.0" prefWidth="68.0" text="5" GridPane.columnIndex="1"
                    GridPane.rowIndex="2" onAction="#handleButtonAction"/>
            <Button fx:id="six" prefHeight="73.0" prefWidth="83.0" text="6" GridPane.columnIndex="2"
                    GridPane.rowIndex="2" onAction="#handleButtonAction"/>
            <Button fx:id="minus" prefHeight="66.0" prefWidth="76.0" text="-" GridPane.columnIndex="3"
                    GridPane.rowIndex="2" onAction="#handleButtonAction"/>
            <Button fx:id="one" prefHeight="90.0" prefWidth="98.0" text="1" GridPane.rowIndex="3"
                    onAction="#handleButtonAction"/>
            <Button fx:id="two" prefHeight="92.0" prefWidth="91.0" text="2" GridPane.columnIndex="1"
                    GridPane.rowIndex="3" onAction="#handleButtonAction"/>
            <Button fx:id="three" prefHeight="54.0" prefWidth="66.0" text="3" GridPane.columnIndex="2"
                    GridPane.rowIndex="3" onAction="#handleButtonAction"/>
            <Button fx:id="plus" prefHeight="71.0" prefWidth="77.0" text="+" GridPane.columnIndex="3"
                    GridPane.rowIndex="3" onAction="#handleButtonAction"/>
        </GridPane>
        <HBox prefHeight="59.0" prefWidth="243.0">
            <Button fx:id="zero" prefHeight="59.0" prefWidth="122.0" text="0" onAction="#handleButtonAction"/>
            <Button fx:id="dot" prefHeight="59.0" prefWidth="60.0" text="." onAction="#handleButtonAction"/>
            <Button fx:id="equal" prefHeight="59.0" prefWidth="63.0" text="=" onAction="#handleButtonAction"/>
        </HBox>
    </VBox>
</AnchorPane>

MainController.java

package allen.edu.com;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

public class MainController implements Initializable {

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        System.out.println("Success");
    }

    private double data = 0.0;
    private String operation = "AC";

    private double readData(String input) {
        if (input.indexOf(".") != input.length()-1) {
            return Double.parseDouble(input);
        }
        return Double.parseDouble(input.substring(0,input.length()-2));
    }

    @FXML
    private Button dot;

    @FXML
    private Button one;

    @FXML
    private Button two;

    @FXML
    private Button three;

    @FXML
    private Button four;

    @FXML
    private Button five;

    @FXML
    private Button six;

    @FXML
    private Button seven;

    @FXML
    private Button eight;

    @FXML
    private Button nine;

    @FXML
    private Button AC;

    @FXML
    private Button minus;

    @FXML
    private Button plus;

    @FXML
    private Button percent;

    @FXML
    private Button divide;

    @FXML
    private Button multiply;

    @FXML
    private Button invert;

    @FXML
    private Label output;

    @FXML
    private Button zero;

    @FXML
    private Button equal;

    @FXML
    public void handleButtonAction(ActionEvent event) {
        if (event.getSource() == one) {
            output.setText(output.getText()+"1");
        } else if (event.getSource() == two) {
            output.setText(output.getText()+"2");
        } else if (event.getSource() == three) {
            output.setText(output.getText()+"3");
        } else if (event.getSource() == four) {
            output.setText(output.getText()+"4");
        } else if (event.getSource() == five) {
            output.setText(output.getText()+"5");
        } else if (event.getSource() == six) {
            output.setText(output.getText()+"6");
        } else if (event.getSource() == seven) {
            output.setText(output.getText()+"7");
        } else if (event.getSource() == eight) {
            output.setText(output.getText()+"8");
        } else if (event.getSource() == nine) {
            output.setText(output.getText()+"9");
        } else if (event.getSource() == zero) {
            if (!output.getText().equals("0"))
                output.setText(output.getText()+"0");
        } else if (event.getSource() == AC) {
            output.setText("");
            operation = "AC";
        } else if (event.getSource() == divide) {
            operation = "Divide";
            data = readData(output.getText());
            output.setText("");
        } else if (event.getSource() == multiply) {
            operation = "Multiply";
            data = readData(output.getText());
            output.setText("");
        } else if (event.getSource() == equal) {
            double Secondhand = readData(output.getText());
            switch (operation) {
                case "Multiply":
                    data *= Secondhand;
                    break;
                case "Minus":
                    data -= Secondhand;
                    break;
                case "Divide":
                    if (Secondhand == 0) throw new IllegalArgumentException("Argument 'divisor' is 0");
                    data /= Secondhand;
                    break;
                case "Plus":
                    data += Secondhand;
                    break;
            }
            output.setText(data+"");
        } else if (event.getSource() == plus) {
            operation = "Plus";
            data = readData(output.getText());
            output.setText("");
        } else if (event.getSource() == minus) {
            operation = "Minus";
            data = readData(output.getText());
            output.setText("");
        } else if (event.getSource() == percent) {
            operation = "Percent";
            data = readData(output.getText());
            data /= 100;
            output.setText(data+"");
        } else if (event.getSource() == dot) {
            output.setText(data+".");
        } else if (event.getSource() == invert) {
            output.setText(""+output.getText());
        }

    }

}

谢谢。

字符串属性中的“%”符号是 FXML 中的特殊字符,解释为指定 resource nameFXMLLoader 正在尝试查找资源(但由于字符串终止立即没有资源名称:因此是例外)。

您可以使用 "\%" 或使用这样的表达式来包含文字 % 作为 '${"%"}'.

<Button fx:id="percent" prefHeight="55.0" prefWidth="65.0" text="\%" GridPane.columnIndex="2"
                onAction="#handleButtonAction"/>