当前位置: 首页 > 知识库问答 >
问题:

JavaFX——导致大量错误的按钮操作

谢弘阔
2023-03-14

我对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>

知道是什么导致了这种情况的发生吗?

共有1个答案

岳承悦
2023-03-14

例外说明了一切:

Caused by: java.lang.NumberFormatException: empty String

这发生在:

Float.parseFloat(result.getText());

基本上你应该重做你的逻辑。例如,在清除操作过程中,不要重置操作器。

 类似资料:
  • 当我移除Android:BackgroundTint标记时,错误就消失了。知道为什么会这样吗?

  • 问题内容: 由于某些原因,我的fx:id无法正确绑定到我的类,因此始终会导致错误。 控制者 XML文件 我认为我了解问题的根源,但不了解如何正确解决。根据这个问题的答案,我认为我正在尝试在调用构造函数之前分配FXML元素(并且这些元素只能在初始化期间/之后分配)。 有没有办法 不 执行此操作?还是我犯了一个完全不同的错误? 问题答案: 您使用进口 在您的fxml文件中。 因此,在加载fxml文件时

  • 我想我明白我的问题的根源,但我不明白如何正确地解决它。根据这个问题的答案,我认为我试图在调用构造函数之前分配FXML元素(这些元素只能在初始化期间/之后分配)。 有没有一种方法可以在不实现的情况下做到这一点?还是我犯了一个完全不同的错误?

  • 当使用浮动动作按钮时,我的Android应用程序崩溃并抛出错误。当我移除它时,我的应用程序成功运行,但如果可以的话,我宁愿使用浮动动作按钮。 这是我的完整错误日志:

  • 我正在尝试使用AJAX将页面中的大量数据传递给PHP脚本。这对于一些少量的数据来说很好,但是当有更多的数据时会出现以下错误。 我正在使用的JQuery是: 数据实际上是一个json数据字符串。 并在PHP中读取,只需使用: 我怎么能传递大量的数据链接这个,但仍然使用$_POST在PHP中引用它 我以为我需要用JSON做一些事情,但我可以弄清楚什么。 谢啦