21xrx.com
2024-12-23 02:15:16 Monday
登录
文章检索 我的文章 写文章
用Java实现简单的计算器
2023-06-13 03:58:57 深夜i     --     --
Java编程 算术运算 GUI实现

文章:

在编程语言中,算术运算是一个基本的部分。在Java中实现简单的计算器是一种非常简单的方法,用于理解Java编程语言中算术运算的基本概念。为了实现这个计算器,我们需要创建一个Java类,它具有基本的GUI(图形用户界面)组件,例如文本框、按钮和标签,以便用户可以输入数字,并得出算术结果。

下面是一个基本的Java代码,用于实现一个简单的计算器:


import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class Calculator implements ActionListener {

  JTextField textField;

  JButton[] numberButtons = new JButton[10];

  JButton[] functionButtons = new JButton[5];

  JButton addButton, subtractButton, multiplyButton, divideButton;

  JButton decimalButton, equalButton, deleteButton, clearButton;

  JPanel panel;

  double num1, num2, result;

  int addClick = 0, subtractClick = 0, multiplyClick = 0, divideClick = 0;

  Calculator() {

    JFrame frame = new JFrame("Calculator");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setSize(500, 500);

    frame.setLocationRelativeTo(null);

    textField = new JTextField();

    textField.setBounds(50, 25, 400, 50);

    textField.setEditable(false);

    addButton = new JButton("+");

    subtractButton = new JButton("-");

    multiplyButton = new JButton("*");

    divideButton = new JButton("/");

    decimalButton = new JButton(".");

    equalButton = new JButton("=");

    deleteButton = new JButton("Delete");

    clearButton = new JButton("Clear");

    functionButtons[0] = addButton;

    functionButtons[1] = subtractButton;

    functionButtons[2] = multiplyButton;

    functionButtons[3] = divideButton;

    functionButtons[4] = equalButton;

    for (int i = 0; i < 10; i++) {

      numberButtons[i] = new JButton(String.valueOf(i));

    }

    panel = new JPanel();

    panel.setBounds(50, 100, 400, 300);

    panel.setLayout(new GridLayout(4, 4, 10, 10));

    panel.add(numberButtons[1]);

    panel.add(numberButtons[2]);

    panel.add(numberButtons[3]);

    panel.add(addButton);

    panel.add(numberButtons[4]);

    panel.add(numberButtons[5]);

    panel.add(numberButtons[6]);

    panel.add(subtractButton);

    panel.add(numberButtons[7]);

    panel.add(numberButtons[8]);

    panel.add(numberButtons[9]);

    panel.add(multiplyButton);

    panel.add(decimalButton);

    panel.add(numberButtons[0]);

    panel.add(deleteButton);

    panel.add(divideButton);

    panel.add(clearButton);

    panel.add(equalButton);

    for (JButton button : functionButtons) {

      button.addActionListener(this);

      button.setFont(button.getFont().deriveFont(20.0f));

      button.setFocusable(false);

    }

    for (JButton button : numberButtons) {

      button.addActionListener(this);

      button.setFont(button.getFont().deriveFont(20.0f));

      button.setFocusable(false);

    }

    decimalButton.addActionListener(this);

    deleteButton.addActionListener(this);

    clearButton.addActionListener(this);

    frame.add(panel);

    frame.add(textField);

    frame.setLayout(null);

    frame.setVisible(true);

  }

  public static void main(String[] args) {

    new Calculator();

  }

  @Override

  public void actionPerformed(ActionEvent e) {

    for (int i = 0; i < 10; i++) {

      if (e.getSource() == numberButtons[i]) {

        textField.setText(textField.getText().concat(String.valueOf(i)));

      }

    }

    if (e.getSource() == decimalButton) {

      textField.setText(textField.getText().concat("."));

    }

    if (e.getSource() == addButton) {

      num1 = Double.parseDouble(textField.getText());

      textField.setText("");

      addClick = 1;

    }

    if (e.getSource() == subtractButton) {

      num1 = Double.parseDouble(textField.getText());

      textField.setText("");

      subtractClick = 1;

    }

    if (e.getSource() == multiplyButton) {

      num1 = Double.parseDouble(textField.getText());

      textField.setText("");

      multiplyClick = 1;

    }

    if (e.getSource() == divideButton) {

      num1 = Double.parseDouble(textField.getText());

      textField.setText("");

      divideClick = 1;

    }

    if (e.getSource() == equalButton) {

      num2 = Double.parseDouble(textField.getText());

      if (addClick > 0) {

        result = num1 + num2;

        addClick = 0;

      }

      if (subtractClick > 0)

        result = num1 - num2;

        subtractClick = 0;

      

      if (multiplyClick > 0) {

        result = num1 * num2;

        multiplyClick = 0;

      }

      if (divideClick > 0)

        result = num1 / num2;

        divideClick = 0;

      

      textField.setText(String.valueOf(result));

    }

    if (e.getSource() == clearButton) {

      textField.setText("");

    }

    if (e.getSource() == deleteButton) {

      String currentText = textField.getText();

      textField.setText("");

      for (int i = 0; i < currentText.length() - 1; i++) {

        textField.setText(textField.getText() + currentText.charAt(i));

      }

    }

  }

}

这个计算器GUI具有数字按钮,四个基本的算术运算符,一个小数点和一些功能按钮。当用户按下一个数字按钮时,数字将被输入到文本框中。当用户按下算术运算符时,计算器将记录运算符并清空文本框。然后,用户可以输入下一个数字或按=运算符进行计算。计算结果将显示在文本框中。

通过这个简单的Java代码,我们可以了解如何在Java中创建GUI,并在其中使用基本的算术运算符。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复