JavaFX加载异常

时间:2014-10-01 14:44:55

标签: java controller javafx fxml fxmlloader

我尝试使用JavaFX制作基本的计算器应用程序,但我在启动应用程序时遇到了问题。

这是我得到的错误

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$48/815033865.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 

/path/.../view/CalculatorScreen.fxml:10

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2403)
at calculator.Calculator.start(Calculator.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/1284973402.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$44/584634336.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1127003017.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/501263526.run(Unknown Source)
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$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/96639997.run(Unknown Source)
... 1 more
Caused by: java.lang.IllegalArgumentException: Can not set java.awt.Button field controller.CalculatorScreenController.zeroButton to javafx.scene.control.Button
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:758)
at javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1155)
at javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:104)
at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:853)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:747)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2701)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2521)
... 17 more
Exception running application calculator.Calculator

这就是我的软件包的设置方式。

calculator
    Calculator.java
controller
    CalculatorScreenController.java
view
    CalculatorScreen.fxml

我的Calculator.java

package calculator;

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

public class Calculator extends Application {

@Override
public void start(Stage stage) throws Exception
{
    FXMLLoader fxmlLoader =  new FXMLLoader( getClass().getResource( "/view/CalculatorScreen.fxml" ));
    Parent root = fxmlLoader.load();
    CalculatorScreenController controller = (CalculatorScreenController) fxmlLoader.getController();

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

我的CalculatorScreenController.java

package controller;

import calculator.Calculator;
import java.awt.Button;
import java.awt.Label;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

public class CalculatorScreenController implements Initializable{

@FXML
private Button oneButton;
@FXML
private Button twoButton;
@FXML
private Button threeButton;
@FXML
private Button fourButton;
@FXML
private Button fiveButton;
@FXML
private Button sixButton;
@FXML
private Button sevenButton;
@FXML
private Button eightButton;
@FXML
private Button nineButton;
@FXML
private Button zeroButton;

@FXML
private Button dotButton;
@FXML
private Button plusButton;
@FXML
private Button minusButton;
@FXML
private Button multiplyButton;
@FXML
private Button divideButton;
@FXML
private Button equalsButton;

@FXML
private Button clearButton;

@FXML
private Label displayLabel;

@Override
public void initialize(URL url, ResourceBundle rb) {
    displayLabel.setText("500");
}
}

我知道我还没有编写太多代码,截至目前我只是想弄清楚如何运行该应用程序。我对这一切都很陌生,所以任何帮助都会受到赞赏,谢谢!

2 个答案:

答案 0 :(得分:2)

您的控制器中输入错误。 java.awt.Buttonjava.awt.Label应为javafx.scene.control.Buttonjavafx.scene.control.Label

答案 1 :(得分:0)

替换此fxml路径 -

  

" /view/CalculatorScreen.fxml"

用这个 -

  

" ../视图/ CalculatorScreen.fxml"