我仍然是JavaFX的新手,目前正在学习在线教程。我一直在尝试为类项目构建一个计算器,并且在大多数情况下,它似乎工作......直到按下清除按钮。按下后,清除按钮会使所有操作员按钮不再起作用,当用户尝试单击一个按钮时,我会收到很大的错误。
以下是此错误列表:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: 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:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
at java.lang.Float.parseFloat(Float.java:451)
at calc.Controller.processOperators(Controller.java:46)
... 58 more
以下是我的.java类:
//MAIN CLASS
package calc;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/calc/Main.fxml"));
Scene scene = new Scene(root);
// scene.getStylesheets().add(getClass().getResource("calc.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e){
e.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
// CONTROLLER CLASS
package calc;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
public class Controller {
@FXML
private Label result;
private float number1 = 0;
private String operator = "";
private boolean start = true;
private Model model = new Model();
@FXML
public void processNumbers(ActionEvent event){
if (start) {
result.setText("");
start = false;
}
String value = ((Button)event.getSource()).getText();
result.setText(result.getText()+value);
}
@FXML
public void processOperators(ActionEvent event){
String value = ((Button)event.getSource()).getText();
if(!value.equals("=")){
if(!operator.isEmpty())
return;
operator = value;
number1 = Float.parseFloat(result.getText());
result.setText("");
} else {
if(operator.isEmpty())
return;
float number2 = Float.parseFloat(result.getText());
float output = model.Calculate(number1, number2, operator);
result.setText(String.valueOf(output));
operator = "";
start = true;
} }
@FXML
public void processClear(ActionEvent event){
result.setText("");
}
}
// MODEL CLASS
package calc;
/**
*
* @author valentyne
*/
public class Model {
public float Calculate(float number1, float 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;
}
}
}
这是我的FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox alignment="CENTER" maxHeight="390.0" maxWidth="300.0" minHeight="390" minWidth="300" prefHeight="390.0" prefWidth="300.0" spacing="10.0" style="-fx-background-color: e1ffa7; -fx-border-style: solid; -fx-border-color: afd95e; -fx-border-width: 3; -fx-background-image: url(/calc/starbg.jpg); -fx-background-repeat: repeat;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="calc.Controller">
<children>
<StackPane prefHeight="50.0" prefWidth="200.0">
<children>
<Label fx:id="result" opacity="0.75" prefHeight="27.0" prefWidth="233.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-border-width: 1; -fx-padding: 5px; -fx-background-color: dafb9b;" textAlignment="RIGHT" textFill="#63861a" StackPane.alignment="TOP_CENTER">
<font>
<Font size="18.0" />
</font>
<StackPane.margin>
<Insets />
</StackPane.margin>
</Label>
</children>
</StackPane>
<HBox alignment="CENTER" prefHeight="50.0" prefWidth="300.0" spacing="10.0">
<children>
<Button mnemonicParsing="false" onAction="#processNumbers" opacity="0.75" prefWidth="50.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-background-color: d2fe7e;" text="7" textFill="#63861a">
<font>
<Font size="18.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#processNumbers" opacity="0.75" prefWidth="50.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-background-color: d2fe7e;" text="8" textFill="#63861a">
<font>
<Font size="18.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#processNumbers" opacity="0.75" prefWidth="50.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-background-color: d2fe7e;" text="9" textFill="#63861a">
<font>
<Font size="18.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#processOperators" opacity="0.75" prefWidth="50.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-background-color: d2fe7e;" text="/" textFill="#63861a">
<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" opacity="0.75" prefWidth="50.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-background-color: d2fe7e;" text="4" textFill="#63861a">
<font>
<Font size="18.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#processNumbers" opacity="0.75" prefWidth="50.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-background-color: d2fe7e;" text="5" textFill="#63861a">
<font>
<Font size="18.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#processNumbers" opacity="0.75" prefWidth="50.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-background-color: d2fe7e;" text="6" textFill="#63861a">
<font>
<Font size="18.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#processOperators" opacity="0.75" prefWidth="50.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-background-color: d2fe7e;" text="*" textFill="#63861a">
<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" opacity="0.75" prefWidth="50.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-background-color: d2fe7e;" text="1" textFill="#63861a">
<font>
<Font size="18.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#processNumbers" opacity="0.75" prefWidth="50.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-background-color: d2fe7e;" text="2" textFill="#63861a">
<font>
<Font size="18.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#processNumbers" opacity="0.75" prefWidth="50.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-background-color: d2fe7e;" text="3" textFill="#63861a">
<font>
<Font size="18.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#processOperators" opacity="0.75" prefWidth="50.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-background-color: d2fe7e;" text="-" textFill="#63861a">
<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" opacity="0.75" prefWidth="50.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-background-color: d2fe7e;" text="0" textFill="#63861a">
<font>
<Font size="18.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#processClear" opacity="0.75" prefWidth="50.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-background-color: d2fe7e;" text="C" textFill="#63861a">
<font>
<Font size="18.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#processOperators" opacity="0.75" prefWidth="50.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-background-color: d2fe7e;" text="=" textFill="#63861a">
<font>
<Font size="18.0" />
</font>
</Button>
<Button mnemonicParsing="false" onAction="#processOperators" opacity="0.75" prefWidth="50.0" style="-fx-border-style: solid; -fx-border-color: afd95e; -fx-background-color: d2fe7e;" text="+" textFill="#63861a">
<font>
<Font size="18.0" />
</font>
</Button>
</children>
</HBox>
</children>
</VBox>
关于导致这种情况发生的原因的任何想法?
答案 0 :(得分:0)
例外说明了一切:
Caused by: java.lang.NumberFormatException: empty String
发生在:
Float.parseFloat(result.getText());
基本上你应该重写你的逻辑。你e。 G。在清除操作期间不要重置操作员。