No first button compression after programme launch



  • All https://github.com/Hokins87/Calculator ♪

    After the program is launched, the first tap is not tracked. If you use the commented lines and change fxml file, everything works correctly.

    Can you tell me what's the problem?

    Is it possible to create only one copy of the class MyEvent() for all buttons, not like I have in my code for method. btnClick(ActionEvent event)?

    public class MyController implements Initializable {
    

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    private double oper1 = 0, oper2 = 0;
    private String operation = "";

    @FXML
    private Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;

    @FXML
    private Button btnClear, btnPoint, btnResult, btnDivide, btnSum, btnSub, btnMultiply;

    @FXML
    private TextField txtField;

    @FXML
    private AnchorPane lblDisplay;

    public void btnClick(ActionEvent event) {
    btn0.setOnAction(new MyEvent());
    btn1.setOnAction(new MyEvent());
    btn2.setOnAction(new MyEvent());
    btn3.setOnAction(new MyEvent());
    btn4.setOnAction(new MyEvent());
    btn5.setOnAction(new MyEvent());
    btn6.setOnAction(new MyEvent());
    btn7.setOnAction(new MyEvent());
    btn8.setOnAction(new MyEvent());
    btn9.setOnAction(new MyEvent());
    }

    /*

    • public void addNum0(ActionEvent event) {
    • if(btn0 == event.getSource()){
    • if(txtField.getText().equals("0")){
    • txtField.setText(txtField.getText() +
    • ""); }else{ txtField.setText(txtField.getText() + "0");
    • System.out.println("0"); } } }
    • public void addNum1(ActionEvent event) {
    • if(txtField.getText().equals("0")){ txtField.setText(""); }
    • txtField.setText(txtField.getText() + "1"); System.out.println("1"); }
    • public void addNum2(ActionEvent event) {
    • if(txtField.getText().equals("0")){ txtField.setText(""); }
    • txtField.setText(txtField.getText() + "2"); System.out.println("2"); }
    • public void addNum3(ActionEvent event) {
    • if(txtField.getText().equals("0")){ txtField.setText(""); }
    • txtField.setText(txtField.getText() + "3"); System.out.println("3"); }
    • public void addNum4(ActionEvent event) {
    • if(txtField.getText().equals("0")){ txtField.setText(""); }
    • txtField.setText(txtField.getText() + "4"); System.out.println("4"); }
    • public void addNum5(ActionEvent event) {
    • if(txtField.getText().equals("0")){ txtField.setText(""); }
    • txtField.setText(txtField.getText() + "5"); System.out.println("5"); }
    • public void addNum6(ActionEvent event) {
    • if(txtField.getText().equals("0")){ txtField.setText(""); }
    • txtField.setText(txtField.getText() + "6"); System.out.println("6"); }
    • public void addNum7(ActionEvent event) {
    • if(txtField.getText().equals("0")){ txtField.setText(""); }
    • txtField.setText(txtField.getText() + "7"); System.out.println("7"); }
    • public void addNum8(ActionEvent event) {
    • if(txtField.getText().equals("0")){ txtField.setText(""); }
    • txtField.setText(txtField.getText() + "8"); System.out.println("8"); }
    • public void addNum9(ActionEvent event) {
    • if(txtField.getText().equals("0")){ txtField.setText(""); }
    • txtField.setText(txtField.getText() + "9"); System.out.println("9"); }
      */

    public void clearDisplay(ActionEvent event) {
    System.out.println("Clear");
    txtField.setText("0");
    }

    public void addDivide(ActionEvent event) {
    System.out.println("Divide");
    oper1 = Double.valueOf(txtField.getText());
    txtField.setText("");
    operation = "/";
    }

    public void addMultiply(ActionEvent event) {
    System.out.println("Multiply");
    oper1 = Double.valueOf(txtField.getText());
    txtField.setText("");
    operation = "*";
    }

    public void addSubtract(ActionEvent event) {
    System.out.println("Subtract");
    oper1 = Double.valueOf(txtField.getText());
    txtField.setText("");
    operation = "-";
    }

    public void addSum(ActionEvent event) {
    System.out.println("Sum");
    oper1 = Double.valueOf(txtField.getText());
    txtField.setText("");
    operation = "+";
    }

    public void addPoint(ActionEvent event) {
    if (btnPoint == event.getSource()) {
    if (!txtField.getText().contains(".")) {
    txtField.setText(txtField.getText() + ".");
    System.out.println("Point");
    }
    }
    }

    public void result(ActionEvent event) {
    MathCalculator math = new MathCalculator();
    oper2 = Double.valueOf(txtField.getText());
    txtField.setText("");
    if (operation.equals("/")) {
    math.divide(oper1, oper2);
    } else if (operation.equals("*")) {
    math.multiply(oper1, oper2);
    } else if (operation.equals("-")) {
    math.subtract(oper1, oper2);
    } else {
    math.sum(oper1, oper2);
    }
    txtField.setText(String.valueOf(oper1) + " " + operation + " " + String.valueOf(oper2) + " = "
    + String.valueOf(math.getResult()));
    }

    class MyEvent implements EventHandler<ActionEvent> {

    @Override
    public void handle(ActionEvent event) {
        Button btn = (Button) event.getSource();
        if (txtField.getText().equals("0")) {
            txtField.setText("");
        }
        txtField.setText(txtField.getText() + btn.getText());
        switch (btn.getText()) {
        case "0":
            System.out.println("0");
            break;
        case "1":
            System.out.println("1");
            break;
        case "2":
            System.out.println("2");
            break;
        case "3":
            System.out.println("3");
            break;
        case "4":
            System.out.println("4");
            break;
        case "5":
            System.out.println("5");
            break;
        case "6":
            System.out.println("6");
            break;
        case "7":
            System.out.println("7");
            break;
        case "8":
            System.out.println("8");
            break;
        case "9":
            System.out.println("9");
            break;
        }
    }
    

    }

    <?xml version="1.0" encoding="UTF-8"?>
    <?import javafx.scene.control.?>
    <?import java.lang.
    ?>
    <?import javafx.scene.layout.?>
    <?import javafx.scene.layout.AnchorPane?>
    <AnchorPane fx:id="lblDisplay" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="290.0" prefWidth="257.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.MyController">
    <children>
    <GridPane layoutX="27.0" layoutY="79.0" prefHeight="200.0" prefWidth="240.0">
    <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
    </columnConstraints>
    <rowConstraints>
    <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
    </rowConstraints>
    <children>
    <Button fx:id="btn8" layoutX="102.0" layoutY="108.0" mnemonicParsing="false" onAction="#btnClick" text="8" GridPane.columnIndex="1" />
    <Button fx:id="btn9" layoutX="172.0" layoutY="108.0" mnemonicParsing="false" onAction="#btnClick" prefHeight="25.0" prefWidth="25.0" text="9" GridPane.columnIndex="2" />
    <Button fx:id="btn7" layoutX="31.0" layoutY="108.0" mnemonicParsing="false" onAction="#btnClick" prefHeight="25.0" prefWidth="25.0" text="7" />
    <Button fx:id="btn2" layoutX="102.0" layoutY="185.0" mnemonicParsing="false" onAction="#btnClick" text="2" GridPane.columnIndex="1" GridPane.rowIndex="2" />
    <Button fx:id="btn1" layoutX="31.0" layoutY="185.0" mnemonicParsing="false" onAction="#btnClick" prefHeight="25.0" prefWidth="25.0" text="1" GridPane.rowIndex="2" />
    <Button fx:id="btn4" layoutX="31.0" layoutY="144.0" mnemonicParsing="false" onAction="#btnClick" prefHeight="25.0" prefWidth="25.0" text="4" GridPane.rowIndex="1" />
    <Button fx:id="btn0" layoutX="102.0" layoutY="228.0" mnemonicParsing="false" onAction="#btnClick" prefHeight="25.0" prefWidth="25.0" text="0" GridPane.columnIndex="1" GridPane.rowIndex="3" />
    <Button fx:id="btnResult" layoutX="172.0" layoutY="228.0" mnemonicParsing="false" onAction="#result" prefHeight="25.0" prefWidth="25.0" text="=" GridPane.columnIndex="2" GridPane.rowIndex="3" />
    <Button fx:id="btn3" layoutX="172.0" layoutY="185.0" mnemonicParsing="false" onAction="#btnClick" prefHeight="25.0" prefWidth="25.0" text="3" GridPane.columnIndex="2" GridPane.rowIndex="2" />
    <Button fx:id="btn5" layoutX="102.0" layoutY="144.0" mnemonicParsing="false" onAction="#btnClick" text="5" GridPane.columnIndex="1" GridPane.rowIndex="1" />
    <Button fx:id="btnSum" layoutX="240.0" layoutY="228.0" mnemonicParsing="false" onAction="#addSum" prefHeight="25.0" prefWidth="25.0" text="+" GridPane.columnIndex="3" GridPane.rowIndex="3" />
    <Button fx:id="btnPoint" layoutX="31.0" layoutY="228.0" mnemonicParsing="false" onAction="#addPoint" prefHeight="25.0" prefWidth="25.0" text="." GridPane.rowIndex="3" />
    <Button fx:id="btnDivide" layoutX="240.0" layoutY="108.0" mnemonicParsing="false" onAction="#addDivide" prefHeight="25.0" prefWidth="25.0" text="/" GridPane.columnIndex="3" />
    <Button fx:id="btn6" layoutX="172.0" layoutY="144.0" mnemonicParsing="false" onAction="#btnClick" prefHeight="25.0" prefWidth="25.0" text="6" GridPane.columnIndex="2" GridPane.rowIndex="1" />
    <Button fx:id="btnMultiply" layoutX="240.0" layoutY="144.0" mnemonicParsing="false" onAction="#addMultiply" prefHeight="25.0" prefWidth="25.0" text="
    " GridPane.columnIndex="3" GridPane.rowIndex="1" />
    <Button fx:id="btnSub" layoutX="240.0" layoutY="185.0" mnemonicParsing="false" onAction="#addSubtract" prefHeight="25.0" prefWidth="25.0" text="-" GridPane.columnIndex="3" GridPane.rowIndex="2" />
    </children>
    </GridPane>
    <Button fx:id="btnClear" layoutX="9.0" layoutY="54.0" mnemonicParsing="false" onAction="#clearDisplay" prefHeight="25.0" prefWidth="240.0" text="Clear" />
    <TextField fx:id="txtField" alignment="TOP_RIGHT" editable="false" layoutX="9.0" layoutY="14.0" prefHeight="25.0" prefWidth="240.0" text="0" />
    </children>
    </AnchorPane>



  • The trouble is, you have a processor hanging on the buttons only after you threw it on the button. That's why you're not working at once, but only after the second strike.

    In this case, if you want to hang your MyEvent processor, you don't have to sign in. fxml Processor onClick♪ So that there's one copy, make it one time and get on the buttons, like in the method. initialize()

    In your case, the right thing. fxml It'll look like:

    <children>
    <Button fx:id="btn8" layoutX="102.0" layoutY="108.0" mnemonicParsing="false" text="8" GridPane.columnIndex="1" />
    <Button fx:id="btn9" layoutX="172.0" layoutY="108.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="25.0" text="9" GridPane.columnIndex="2" />
    <Button fx:id="btn7" layoutX="31.0" layoutY="108.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="25.0" text="7" />
    <Button fx:id="btn2" layoutX="102.0" layoutY="185.0" mnemonicParsing="false" text="2" GridPane.columnIndex="1" GridPane.rowIndex="2" />
    <Button fx:id="btn1" layoutX="31.0" layoutY="185.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="25.0" text="1" GridPane.rowIndex="2" />
    <Button fx:id="btn4" layoutX="31.0" layoutY="144.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="25.0" text="4" GridPane.rowIndex="1" />
    <Button fx:id="btn0" layoutX="102.0" layoutY="228.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="25.0" text="0" GridPane.columnIndex="1" GridPane.rowIndex="3" />
    <Button fx:id="btnResult" layoutX="172.0" layoutY="228.0" mnemonicParsing="false" onAction="#result" prefHeight="25.0" prefWidth="25.0" text="=" GridPane.columnIndex="2" GridPane.rowIndex="3" />
    <Button fx:id="btn3" layoutX="172.0" layoutY="185.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="25.0" text="3" GridPane.columnIndex="2" GridPane.rowIndex="2" />
    <Button fx:id="btn5" layoutX="102.0" layoutY="144.0" mnemonicParsing="false" text="5" GridPane.columnIndex="1" GridPane.rowIndex="1" />
    <Button fx:id="btnSum" layoutX="240.0" layoutY="228.0" mnemonicParsing="false" onAction="#addSum" prefHeight="25.0" prefWidth="25.0" text="+" GridPane.columnIndex="3" GridPane.rowIndex="3" />
    <Button fx:id="btnPoint" layoutX="31.0" layoutY="228.0" mnemonicParsing="false" onAction="#addPoint" prefHeight="25.0" prefWidth="25.0" text="." GridPane.rowIndex="3" />
    <Button fx:id="btnDivide" layoutX="240.0" layoutY="108.0" mnemonicParsing="false" onAction="#addDivide" prefHeight="25.0" prefWidth="25.0" text="/" GridPane.columnIndex="3" />
    <Button fx:id="btn6" layoutX="172.0" layoutY="144.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="25.0" text="6" GridPane.columnIndex="2" GridPane.rowIndex="1" />
    <Button fx:id="btnMultiply" layoutX="240.0" layoutY="144.0" mnemonicParsing="false" onAction="#addMultiply" prefHeight="25.0" prefWidth="25.0" text="*" GridPane.columnIndex="3" GridPane.rowIndex="1" />
    <Button fx:id="btnSub" layoutX="240.0" layoutY="185.0" mnemonicParsing="false" onAction="#addSubtract" prefHeight="25.0" prefWidth="25.0" text="-" GridPane.columnIndex="3" GridPane.rowIndex="2" />
    </children>
    

    That's where we took the liseners off. onClick for all digital buttons.

    Now, let's move on to the evet and set up all the buttons.

    public class MyController implements Initializable {
    

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    MyEvent btnEvent = new MyEvent();
    btn0.setOnAction(btnEvent);
    btn1.setOnAction(btnEvent);
    btn2.setOnAction(btnEvent);
    btn3.setOnAction(btnEvent);
    btn4.setOnAction(btnEvent);
    btn5.setOnAction(btnEvent);
    btn6.setOnAction(btnEvent);
    btn7.setOnAction(btnEvent);
    btn8.setOnAction(btnEvent);
    btn9.setOnAction(btnEvent);
    }

    P. S.

    switch (btn.getText()) {
    case "0":
    System.out.println("0");
    break;
    case "1":
    System.out.println("1");
    break;
    case "2":
    System.out.println("2");
    break;
    case "3":
    System.out.println("3");
    break;
    case "4":
    System.out.println("4");
    break;
    сase "5":
    System.out.println("5");
    break;
    case "6":
    System.out.println("6");
    break;
    case "7":
    System.out.println("7");
    break;
    сase "8":
    System.out.println("8");
    break;
    case "9":
    System.out.println("9");
    break;
    }
    }

    Replacement

    System.out.println(btn.getText());




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2