小编典典

JavaFX begginer的简单计算器事件处理

java

嘿,我是Java的初学者,而且我只是做了很短的时间,无论如何,对于我的Java基础项目,我需要用gui做一个简单的计算器,我想不是那样的辛苦但是我有点错:P我已经尽了最大的努力(我认为),但是在操作的事件处理和设置要计算的值上陷入了atm,这是我的代码,请您给我一些建议或技巧关于如何做到这一点:D

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.WritableObjectValue;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;



public class Calculator extends Application {

    private DoubleProperty value = new SimpleDoubleProperty();

    @Override
    public void start(Stage primaryStage) {

        FlowPane pane = new FlowPane();
        pane.setAlignment(Pos.CENTER);
        pane.setPadding(new Insets ( 30 , 20 , 30 , 20));
        pane.setHgap(5);
        pane.setVgap(5);
        pane.setMinWidth(400);
        pane.setPrefWidth(400);
        pane.setMaxWidth(400);

        TextField Rezultat = new TextField();
        Rezultat.setEditable(false);
        Rezultat.setAlignment(Pos.CENTER);
        Rezultat.setMinSize(336, 40);
        Rezultat.textProperty().bind(Bindings.format("%.0f" , value));
        pane.getChildren().add(Rezultat);

        Button sedmica = new Button("7");           
        Button osmica = new Button("8");
        Button devetka = new Button("9");
        Button plus = new Button("+");

            sedmica.setMinSize(80, 80);
            osmica.setMinSize(80, 80);
            devetka.setMinSize(80, 80);
            plus.setMinSize(80, 80);

    pane.getChildren().add(sedmica);
        sedmicaHandler handler7 = new sedmicaHandler();
        sedmica.setOnAction(handler7);
    pane.getChildren().add(osmica);
        osmicaHandler handler8 = new osmicaHandler();
        osmica.setOnAction(handler8);
    pane.getChildren().add(devetka);
        devetkaHandler handler9 = new devetkaHandler();
        devetka.setOnAction(handler9);
    pane.getChildren().add(plus);
        plusHandler handlerplus = new plusHandler();
        plus.setOnAction(handlerplus);

        Button cetvorka = new Button("4");
        Button petica = new Button("5");
        Button sestica = new Button("6");
        Button minus = new Button("-");

            cetvorka.setMinSize(80, 80);
            petica.setMinSize(80, 80);
            sestica.setMinSize(80, 80);
            minus.setMinSize(80, 80);

    pane.getChildren().add(cetvorka);
        cetvorkaHandler handler4 = new cetvorkaHandler();
        cetvorka.setOnAction(handler4);
    pane.getChildren().add(petica);
        peticaHandler handler5 = new peticaHandler();
        petica.setOnAction(handler5);
    pane.getChildren().add(sestica);
        sesticaHandler handler6 = new sesticaHandler();
        sestica.setOnAction(handler6);
    pane.getChildren().add(minus);
        minusHandler handlerminus = new minusHandler();
        minus.setOnAction(handlerminus);

        Button trica = new Button("3");
        Button dvica = new Button("2");
        Button jedinica = new Button("1");
        Button puta = new Button("*");

            trica.setMinSize(80, 80);
            dvica.setMinSize(80, 80);
            jedinica.setMinSize(80, 80);
            puta.setMinSize(80, 80);

    pane.getChildren().add(jedinica);
        jedinicaHandler handler1 = new jedinicaHandler();
        jedinica.setOnAction(handler1);
    pane.getChildren().add(dvica);
        dvicaHandler handler2 = new dvicaHandler();
        dvica.setOnAction(handler2);
    pane.getChildren().add(trica);
        tricaHandler handler3 = new tricaHandler();
        trica.setOnAction(handler3);
    pane.getChildren().add(puta);
        putaHandler handlerputa = new putaHandler();
        puta.setOnAction(handlerputa);

        Button nula = new Button("0");
        Button jednako = new Button("=");
        Button podijeljeno = new Button("/");
        Button EE = new Button ("EE");

            nula.setMinSize(80, 80);
            jednako.setMinSize(80, 80);
            podijeljeno.setMinSize(80, 80);
            EE.setMinSize(80, 80);

    pane.getChildren().add(EE);
        EEHandler handlerEE = new EEHandler();
        EE.setOnAction(handlerEE);
    pane.getChildren().add(nula);
        nulaHandler handler0 = new nulaHandler();
        nula.setOnAction(handler0);
    pane.getChildren().add(jednako);
        jednakoHandler handlerjednako = new jednakoHandler();
        jednako.setOnAction(handlerjednako);
    pane.getChildren().add(podijeljeno);
        podijeljenoHandler handlerpodijeljeno = new podijeljenoHandler();
        podijeljeno.setOnAction(handlerpodijeljeno);



    Scene scene = new Scene(pane);
    primaryStage.setTitle("Calculator");
    primaryStage.setScene(scene);
    primaryStage.show();

    }

    public static void main (String[] args) {
    Application.launch(args);

    }

}

class nulaHandler implements EventHandler<ActionEvent>{
        @Override
    public void handle(ActionEvent e){
        value.set(0);
    }
}
class jedinicaHandler implements EventHandler<ActionEvent>{
        @Override
    public void handle(ActionEvent e){
        System.out.println("1");
    }
}           
class dvicaHandler implements EventHandler<ActionEvent>{
        @Override
    public void handle(ActionEvent e){
        System.out.println("2");
    }   
}
class tricaHandler implements EventHandler<ActionEvent>{
        @Override
    public void handle(ActionEvent e){
        System.out.println("3");
    }
}           
class cetvorkaHandler implements EventHandler<ActionEvent>{
        @Override
    public void handle(ActionEvent e){
        System.out.println("4");
    }
}
class peticaHandler implements EventHandler<ActionEvent>{
        @Override
    public void handle(ActionEvent e){
        System.out.println("5");
    }
}                   
class sesticaHandler implements EventHandler<ActionEvent>{
        @Override
    public void handle(ActionEvent e){
        System.out.println("6");
    }
}                       
class sedmicaHandler implements EventHandler<ActionEvent>{
        @Override
    public void handle(ActionEvent e){
        System.out.println("7");
    }
}                           
class osmicaHandler implements EventHandler<ActionEvent>{
        @Override
    public void handle(ActionEvent e){
        System.out.println("8");
    }
}                               
class devetkaHandler implements EventHandler<ActionEvent>{
        @Override
    public void handle(ActionEvent e){
        System.out.println("9");
    }
}                                   
class plusHandler implements EventHandler<ActionEvent>{
        @Override
    public void handle(ActionEvent e){
        System.out.println("+");
    }
}                                       
class minusHandler implements EventHandler<ActionEvent>{
        @Override
    public void handle(ActionEvent e){
        System.out.println("-");
    }
}                                           
class putaHandler implements EventHandler<ActionEvent>{
        @Override
    public void handle(ActionEvent e){
        System.out.println("*");
    }
}
class podijeljenoHandler implements EventHandler<ActionEvent>{
        @Override
    public void handle(ActionEvent e){
        System.out.println("/");
    }
}                                                   
class jednakoHandler implements EventHandler<ActionEvent>{
        @Override
    public void handle(ActionEvent e){
        System.out.println("=");
    }
}                                                       
class EEHandler implements EventHandler<ActionEvent>{
        @Override
    public void handle(ActionEvent e){
        System.out.println("EE");
    }
}

我的处理程序只是基本的处理程序,它们将数字打印到控制台中,因此您可以不理会它们,我看到了一些带有value.set命令的示例,我应该使用它们还是?请帮助截止日期为2周。非常感谢


阅读 374

收藏
2020-11-30

共1个答案

小编典典

我不会为您做作业,只是一些技巧:

  • 变量名称应以小写字母开头(TextField rezultat,而不是Rezultat)

考虑一下按下按钮时您期望发生的情况:

  • 如果按5’-‘’+‘55,计算器应该怎么做。应该从5中减去还是加55)
  • 您要计算多个操作吗?(5 + 5 + 5 + 5/5)
  • 十进制数呢?(您无法在当前的设计中填写它们)

您可以为每个按钮执行几乎相同的操作(或者至少有10个按钮具有相同的逻辑),您可以循环创建应用程序:

 public class Calculator extends Application {

 private TextField textField = new TextField();

 @Override
 public void start(Stage primaryStage) {

    List<String> buttons = Arrays.asList("7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", "=", "/", "EE");

    FlowPane pane = new FlowPane();
    pane.setAlignment(Pos.CENTER);
    pane.setPadding(new Insets(30, 20, 30, 20));
    pane.setHgap(5);
    pane.setVgap(5);
    pane.setMinWidth(400);
    pane.setPrefWidth(400);
    pane.setMaxWidth(400);

    textField.setEditable(false);
    textField.setAlignment(Pos.CENTER);
    textField.setMinSize(336, 40);
    // Rezultat.textProperty().bind(Bindings.format("%.0f", value));
    pane.getChildren().add(textField);

    for (String button : buttons) {
        Button b = new Button(button);
        b.setMinSize(80, 80);
        pane.getChildren().add(b);
        b.setOnAction((e) -> doSomething(b.getText()));
    }

    Scene scene = new Scene(pane);
    primaryStage.setTitle("Calculator");
    primaryStage.setScene(scene);
    primaryStage.show();

}

private void doSomething(String text) {

    if (text.equalsIgnoreCase("=")) {
        // Calc
    }

    else if (text.equalsIgnoreCase("EE")) {
        // EE
    } else if (text.equalsIgnoreCase("+") || text.equalsIgnoreCase("-") || text.equalsIgnoreCase("*") || text.equalsIgnoreCase("/")) {
        // + - * /
    }
    // ....
    else {
        // numeric
        textField.appendText(text);
    }
}

public static void main(String[] args) {
    Application.launch(args);

}

}
2020-11-30