Java 高级程序设计:实验四 事件驱动编程

代码地址:Github

实验目的和要求

  1. 掌握匿名类相关知识。

  2. 能够使用事件处理机制。

  3. 能够使用lambda表达式简化事件处理。

实验内容

利用JavaFX编写一个简单计算器功能。

image-20221016211503999

calculator.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.owem.experiment04.CalculatorController">
   <center>
      <BorderPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <top>
            <GridPane prefHeight="80.0" prefWidth="600.0" BorderPane.alignment="CENTER">
               <columnConstraints>
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
               </columnConstraints>
               <rowConstraints>
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
               </rowConstraints>
               <children>
                  <Button mnemonicParsing="false" onAction="#OnBackspaceButton" prefHeight="126.0" prefWidth="200.0" text="Backspace" textFill="RED">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnCEButton" prefHeight="123.0" prefWidth="200.0" text="CE" textFill="RED" GridPane.columnIndex="1">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnCButton" prefHeight="139.0" prefWidth="200.0" text="C" textFill="RED" GridPane.columnIndex="2">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
               </children>
            </GridPane>
         </top>
         <center>
            <GridPane BorderPane.alignment="CENTER">
               <columnConstraints>
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
               </columnConstraints>
               <rowConstraints>
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
               </rowConstraints>
               <children>
                  <Button mnemonicParsing="false" onAction="#OnSevenButton" prefHeight="184.0" prefWidth="234.0" text="7" textFill="#0011ff">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnEightButton" prefHeight="185.0" prefWidth="226.0" text="8" textFill="#0011ff" GridPane.columnIndex="1">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnNineButton" prefHeight="195.0" prefWidth="235.0" text="9" textFill="#0011ff" GridPane.columnIndex="2">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnDivideButton" prefHeight="199.0" prefWidth="228.0" text="/" textFill="RED" GridPane.columnIndex="3">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnSqrtButton" prefHeight="170.0" prefWidth="171.0" text="sqrt" textFill="#0011ff" GridPane.columnIndex="4">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnFourButton" prefHeight="177.0" prefWidth="231.0" text="4" textFill="#0011ff" GridPane.rowIndex="1">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnFiveButton" prefHeight="188.0" prefWidth="192.0" text="5" textFill="#0011ff" GridPane.columnIndex="1" GridPane.rowIndex="1">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnSixButton" prefHeight="184.0" prefWidth="184.0" text="6" textFill="#0011ff" GridPane.columnIndex="2" GridPane.rowIndex="1">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnMultiplicationButton" prefHeight="173.0" prefWidth="178.0" text="*" textFill="RED" GridPane.columnIndex="3" GridPane.rowIndex="1">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnPercentButton" prefHeight="155.0" prefWidth="186.0" text="\%" textFill="#0011ff" GridPane.columnIndex="4" GridPane.rowIndex="1">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnThreeButton" prefHeight="140.0" prefWidth="193.0" text="3" textFill="#0011ff" GridPane.rowIndex="2">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnTwoButton" prefHeight="172.0" prefWidth="195.0" text="2" textFill="#0011ff" GridPane.columnIndex="1" GridPane.rowIndex="2">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnOneButton" prefHeight="160.0" prefWidth="206.0" text="1" textFill="#0011ff" GridPane.columnIndex="2" GridPane.rowIndex="2">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnSubtractButton" prefHeight="145.0" prefWidth="180.0" text="-" textFill="RED" GridPane.columnIndex="3" GridPane.rowIndex="2">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnOneOfButton" prefHeight="160.0" prefWidth="156.0" text="1/x" textFill="#0011ff" GridPane.columnIndex="4" GridPane.rowIndex="2">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnZeroButton" prefHeight="129.0" prefWidth="206.0" text="0" textFill="#0011ff" GridPane.rowIndex="3">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnPositiveOrNegativeButton" prefHeight="151.0" prefWidth="189.0" text="+/-" textFill="#0011ff" GridPane.columnIndex="1" GridPane.rowIndex="3">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnPointButton" prefHeight="176.0" prefWidth="184.0" text="." textFill="#0011ff" GridPane.columnIndex="2" GridPane.rowIndex="3">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnPlusButton" prefHeight="165.0" prefWidth="169.0" text="+" textFill="RED" GridPane.columnIndex="3" GridPane.rowIndex="3">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onAction="#OnEqualButton" prefHeight="216.0" prefWidth="226.0" text="=" textFill="RED" GridPane.columnIndex="4" GridPane.rowIndex="3">
                     <font>
                        <Font name="System Bold" size="30.0" />
                     </font>
                  </Button>
               </children>
            </GridPane>
         </center>
      </BorderPane>
   </center>
   <top>
      <Label fx:id="numberText" alignment="CENTER_RIGHT" contentDisplay="CENTER" prefHeight="75.0" prefWidth="881.0" text="0" textAlignment="RIGHT" BorderPane.alignment="CENTER">
         <font>
            <Font size="30.0" />
         </font>
      </Label>
   </top>
</BorderPane>

CalculatorApplication.java

package com.owem.experiment04;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class CalculatorApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(CalculatorApplication.class.getResource("calculator.fxml"));
        Scene scene = new Scene(fxmlLoader.load());
        stage.setTitle("简易计算器");
        stage.setScene(scene);
        stage.show();
    }

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

CalculatorController.java

package com.owem.experiment04;

import javafx.fxml.FXML;
import javafx.scene.control.Label;

public class CalculatorController {
    @FXML
    private Label numberText;
    double perNumber = 0;
    String operation = "";

    @FXML
    protected void OnBackspaceButton() {
        String str = numberText.getText();
        if (str.charAt(0) == '=') {
            str = "0";
        } else {
            str = str.substring(0, str.length() - 1);
            if (str.length() == 0) {
                str = "0";
            } else if (str.charAt(str.length() - 1) == '.') {
                str = str.substring(0, str.length() - 1);
            }
        }
        numberText.setText(str);
    }

    @FXML
    protected void OnCEButton() {
        numberText.setText("0");
        perNumber = 0;
    }

    @FXML
    protected void OnCButton() {
        numberText.setText("0");
    }

    @FXML
    protected void OnSevenButton() {
        String str = numberText.getText();
        if (str.equals("0") || (!str.equals("") && str.charAt(0) == '=')) {
            str = "";
        }
        str = str + "7";
        numberText.setText(str);
    }

    @FXML
    protected void OnEightButton() {
        String str = numberText.getText();
        if (str.equals("0") || (!str.equals("") && str.charAt(0) == '=')) {
            str = "";
        }
        str = str + "8";
        numberText.setText(str);
    }

    @FXML
    protected void OnNineButton() {
        String str = numberText.getText();
        if (str.equals("0") || (!str.equals("") && str.charAt(0) == '=')) {
            str = "";
        }
        str = str + "9";
        numberText.setText(str);
    }

    @FXML
    protected void OnDivideButton() {
        perNumber = Double.parseDouble(numberText.getText());
        operation = "/";
        numberText.setText("");
    }

    @FXML
    protected void OnSqrtButton() {
        double number = Double.parseDouble(numberText.getText());
        numberText.setText("=" + Math.sqrt(number));
    }

    @FXML
    protected void OnFourButton() {
        String str = numberText.getText();
        if (str.equals("0") || (!str.equals("") && str.charAt(0) == '=')) {
            str = "";
        }
        str = str + "4";
        numberText.setText(str);
    }

    @FXML
    protected void OnFiveButton() {
        String str = numberText.getText();
        if (str.equals("0") || (!str.equals("") && str.charAt(0) == '=')) {
            str = "";
        }
        str = str + "5";
        numberText.setText(str);
    }

    @FXML
    protected void OnSixButton() {
        String str = numberText.getText();
        if (str.equals("0") || (!str.equals("") && str.charAt(0) == '=')) {
            str = "";
        }
        str = str + "6";
        numberText.setText(str);
    }

    @FXML
    protected void OnMultiplicationButton() {
        perNumber = Double.parseDouble(numberText.getText());
        operation = "*";
        numberText.setText("");
    }

    @FXML
    protected void OnPercentButton() {
        double number = Double.parseDouble(numberText.getText());
        if (number != 0) {
            numberText.setText(String.valueOf(number / 100));
        }
    }

    @FXML
    protected void OnThreeButton() {
        String str = numberText.getText();
        if (str.equals("0") || (!str.equals("") && str.charAt(0) == '=')) {
            str = "";
        }
        str = str + "3";
        numberText.setText(str);
    }

    @FXML
    protected void OnTwoButton() {
        String str = numberText.getText();
        if (str.equals("0") || (!str.equals("") && str.charAt(0) == '=')) {
            str = "";
        }
        str = str + "2";
        numberText.setText(str);
    }

    @FXML
    protected void OnOneButton() {
        String str = numberText.getText();
        if (str.equals("0") || (!str.equals("") && str.charAt(0) == '=')) {
            str = "";
        }
        str = str + "1";
        numberText.setText(str);
    }

    @FXML
    protected void OnSubtractButton() {
        perNumber = Double.parseDouble(numberText.getText());
        operation = "-";
        numberText.setText("");
    }

    @FXML
    protected void OnOneOfButton() {
        if (numberText.getText().charAt(0) != '=') {
            double number = Double.parseDouble(numberText.getText());
            if (number != 0) {
                numberText.setText("=" + 1 / number);
            }
        }
    }

    @FXML
    protected void OnZeroButton() {
        String str = numberText.getText();
        if (str.equals("0") || (!str.equals("") && str.charAt(0) == '=')) {
            str = "";
        }
        str = str + "0";
        numberText.setText(str);
    }

    @FXML
    protected void OnPositiveOrNegativeButton() {
        String str = numberText.getText();
        if (!str.equals("0")) {
            if (numberText.getText().contains("-")) {
                numberText.setText(str.substring(1));
            } else {
                numberText.setText("-" + str);
            }
        }
    }

    @FXML
    protected void OnPointButton() {
        if (!numberText.getText().contains(".")) {
            numberText.setText(numberText.getText() + ".");
        }
    }

    @FXML
    protected void OnPlusButton() {
        perNumber = Double.parseDouble(numberText.getText());
        operation = "+";
        numberText.setText("");
    }

    @FXML
    protected void OnEqualButton() {
        double number = Double.parseDouble(numberText.getText());
        switch (operation) {
            case "+" -> number = perNumber + number;
            case "-" -> number = perNumber - number;
            case "*" -> number = perNumber * number;
            case "/" -> {
                if (number != 0) {
                    number = perNumber / number;
                }
            }
        }
        numberText.setText("=" + number);
    }
}