javafx fxml LoadException

时间:2017-09-17 15:23:24

标签: java eclipse javafx fxml

我是JavaFx的新手。我正在尝试按照教程构建一个简单的计算器。我一步一步地按照教程。当我运行该项目时,它显示了javafx fxml LoadException。我看了一些与此相关的解决方案,但我仍无法修复它。请帮我解决这个错误。(很抱歉很长的帖子)

我的程序文件在

下面

MainController.java

package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

public class MainController {

@FXML
private Label result; // label variable shows output above the buttons, check this
private long number1 = 0;
private String operator = "";
private boolean start = true;
private Model model = new Model();

@FXML
public void processNumbers(ActionEvent event) {
    if (start) {
        result.setText(""); // set the label(output) screen as blank first
        start = false;
    }
    String value = ((Button) event.getSource()).getText(); // take number 1-9 and convert it into String, import the
                                                            // button class otherwise will shows error
    result.setText(result.getText() + value);
}

@FXML
public void processOperators(ActionEvent event) {
    String value = ((Button) event.getSource()).getText();`enter code here`

    if (!value.equals("=")) {
        if (!operator.isEmpty()) {
            return;
        }
        operator = value;
        number1 = Long.parseLong(result.getText());
        result.setText("");
    } else {
        if (operator.isEmpty())
            return;
        long number2 = Long.parseLong(result.getText());
        float output = model.calculate(number2, number2, operator); 

        result.setText(String.valueOf(output));
        start = true;
        }
      }

 }

Model.java

 package application;

public class Model { //for some action event 

    public float calculate(long number1,long number2,String operator) {

    switch(operator) {
        case "+":
            return number1+number2;
        case "-":
            return number1-number2;
        case "*":
            return number1*number2;
        case "/":
            if(number2==0) return 0;
            return number1+number2;
        default:
            return 0;

        }//switch case


    }


}

Main.java

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.*;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = (Parent) FXMLLoader.load(getClass().getResource("/application/CalculatorFxmlFile.fxml"));
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

CalculatorFxmlFile

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="300.0" spacing="10.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <children>
      <StackPane prefHeight="50.0" prefWidth="200.0">
         <children>
            <Label fx:id="result" prefHeight="17.0" prefWidth="327.0" text="Label">
               <font>
                  <Font name="System Bold" size="18.0" />
               </font>
            </Label>
         </children></StackPane>
      <HBox alignment="CENTER" prefHeight="50.0" prefWidth="300.0" spacing="10.0">
         <children>
            <Button mnemonicParsing="false" onAction="#processNumbers" prefWidth="50.0" text="7">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#processNumbers" prefWidth="50.0" text="8">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#processNumbers" prefWidth="50.0" text="9">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
            <Button fx:id="result" mnemonicParsing="false" onAction="#processOperators" prefWidth="50.0" text="/">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
         </children>
      </HBox>
      <HBox alignment="CENTER" prefHeight="50.0" prefWidth="300.0" spacing="10.0">
         <children>
            <Button mnemonicParsing="false" onAction="#processNumbers" prefWidth="50.0" text="4">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#processNumbers" prefWidth="50.0" text="5">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#processNumbers" prefWidth="50.0" text="6">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#processOperators" prefWidth="50.0" text="*">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
         </children>
      </HBox>
      <HBox alignment="CENTER" prefHeight="50.0" prefWidth="300.0" spacing="10.0">
         <children>
            <Button mnemonicParsing="false" onAction="#processNumbers" prefWidth="50.0" text="1">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#processNumbers" prefWidth="50.0" text="2">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#processNumbers" prefWidth="50.0" text="3">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#processOperators" prefWidth="50.0" text="-">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
         </children>
      </HBox>
      <HBox alignment="CENTER" prefHeight="50.0" prefWidth="300.0" spacing="10.0">
         <children>
            <Button mnemonicParsing="false" onAction="#processNumbers" prefWidth="110.0" text="0">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#processOperators" prefWidth="50.0" text="=">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#processOperators" prefWidth="50.0" text="+">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
         </children>
      </HBox>
   </children>
</VBox>

生成例外

javafx.fxml.LoadException: 
/C:/AllPrograms/Java/eclipse1/CalculatorJavaFx/bin/application/CalculatorFxmlFile.fxml:37

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    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 application.Main.start(Main.java:15)
    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)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Can not set javafx.scene.control.Label field application.MainController.result to javafx.scene.control.Button
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
    at java.lang.reflect.Field.set(Unknown Source)
    at javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163)
    at javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:751)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    ... 17 more

1 个答案:

答案 0 :(得分:1)

这是您的错误(在您的fxml文件中):

<Button fx:id="result" mnemonicParsing="false" onAction="#processOperators" prefWidth="50.0" text="/">
               <font>
                  <Font size="18.0" />
               </font>
            </Button>

此按钮id是“结果”,也是标签的ID。您应该删除此id属性(我认为您错过了它)或将其更改为教程建议的属性。

<强>更新

此操作是除法,但您要添加数字number1number2

case "/":
    if(number2==0) return 0;
    return number1+number2;

所以你应该进行除法而不是加法

case "/":
    if(number2==0) return 0;
    return number1/number2;

更新2

我发现的另一个错误也在这里:

float output = model.calculate(number2, number2, operator); 

您错过了number1方法中的calculate变量:

float output = model.calculate(number1, number2, operator);