问题描述
我试图用JavaFX制作一个基本的计算器应用程序,但是我无法启动应用程序。
I'm trying to make a basic calculator application with JavaFX but I'm having trouble launching the application.
这是我得到的错误
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
这是我的软件包的设置。
This is how my packages are set up.
calculator
Calculator.java
controller
CalculatorScreenController.java
view
CalculatorScreen.fxml
我的Calculator.java
My 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
My 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");
}
}
我知道我没有写多少代码,现在我只是想弄清楚如何运行应用程序。
I know I don't have much code written yet, as of now I'm just trying to figure out how to run the application. I'm new at all of this so any help would be appreciated, thanks!
推荐答案
您在控制器中有错误的导入。 java.awt.Button
和 java.awt.Label
应为 javafx.scene.control .Button
和 javafx.scene.control.Label
You have the wrong imports in your controller. java.awt.Button
and java.awt.Label
should be javafx.scene.control.Button
and javafx.scene.control.Label
这篇关于JavaFX加载异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!