1

我正在尝试使用 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

我的计算器.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");
}
}

我知道我还没有编写太多代码,到目前为止,我只是想弄清楚如何运行该应用程序。我是新手,所以任何帮助将不胜感激,谢谢!

4

2 回答 2

3

您的控制器中有错误的导入。应该是java.awt.Buttonjava.awt.Labeljavafx.scene.control.Buttonjavafx.scene.control.Label

于 2014-10-01T15:04:40.820 回答
0

替换这个 fxml 路径-

“/view/CalculatorScreen.fxml”

有了这个-

“../view/CalculatorScreen.fxml”

于 2016-09-10T19:21:55.990 回答