21xrx.com
2025-04-25 22:11:45 Friday
文章检索 我的文章 写文章
使用Java实现简单的计算器
2023-06-13 12:39:08 深夜i     9     0
Java 计算器 GUI

Java是一种非常流行的编程语言,可以用来开发各种类型的应用程序。这篇文章将介绍如何使用Java来编写一个简单的计算器程序。我们将演示如何设计和实现一个基本的图形用户界面(GUI),然后添加简单的计算功能。

首先,我们需要一个IDE(集成开发环境),可以使用Eclipse或IntelliJ IDEA等流行的Java IDE。启动IDE,并创建一个新的Java项目。接下来,我们将创建一个GUI,可以在其中为数字按钮和操作符按钮添加事件。

代码示例:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleCalculator extends JFrame implements ActionListener {
  private JTextField field;
  private double num1, num2, result;
  private char operator;
  public SimpleCalculator() {
    setTitle("Simple Calculator");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300, 300);
    setLayout(new BorderLayout());
    field = new JTextField();
    field.setHorizontalAlignment(JTextField.RIGHT);
    add(field, BorderLayout.NORTH);
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(4, 4));
    JButton b0 = new JButton("0");
    JButton b1 = new JButton("1");
    JButton b2 = new JButton("2");
    JButton b3 = new JButton("3");
    JButton b4 = new JButton("4");
    JButton b5 = new JButton("5");
    JButton b6 = new JButton("6");
    JButton b7 = new JButton("7");
    JButton b8 = new JButton("8");
    JButton b9 = new JButton("9");
    JButton bDot = new JButton(".");
    JButton bAdd = new JButton("+");
    JButton bSub = new JButton("-");
    JButton bMul = new JButton("*");
    JButton bDiv = new JButton("/");
    JButton bEq = new JButton("=");
    panel.add(b7);
    panel.add(b8);
    panel.add(b9);
    panel.add(bDiv);
    panel.add(b4);
    panel.add(b5);
    panel.add(b6);
    panel.add(bMul);
    panel.add(b1);
    panel.add(b2);
    panel.add(b3);
    panel.add(bSub);
    panel.add(b0);
    panel.add(bDot);
    panel.add(bEq);
    panel.add(bAdd);
    add(panel, BorderLayout.CENTER);
    b0.addActionListener(this);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
    b6.addActionListener(this);
    b7.addActionListener(this);
    b8.addActionListener(this);
    b9.addActionListener(this);
    bDot.addActionListener(this);
    bAdd.addActionListener(this);
    bSub.addActionListener(this);
    bMul.addActionListener(this);
    bDiv.addActionListener(this);
    bEq.addActionListener(this);
  }
  public void actionPerformed(ActionEvent e) {
    String s = e.getActionCommand();
    if (s.equals("0") || s.equals("1") || s.equals("2") || s.equals("3") || s.equals("4") || s.equals("5") || s.equals("6") || s.equals("7") || s.equals("8") || s.equals("9")) {
      field.setText(field.getText() + s);
    } else if (s.equals(".")) {
      field.setText(field.getText() + ".");
    } else if (s.equals("+")) {
      num1 = Double.parseDouble(field.getText());
      operator = '+';
      field.setText("");
    } else if (s.equals("-")) {
      num1 = Double.parseDouble(field.getText());
      operator = '-';
      field.setText("");
    } else if (s.equals("*")) {
      num1 = Double.parseDouble(field.getText());
      operator = '*';
      field.setText("");
    } else if (s.equals("/")) {
      num1 = Double.parseDouble(field.getText());
      operator = '/';
      field.setText("");
    } else if (s.equals("=")) {
      num2 = Double.parseDouble(field.getText());
      switch (operator) {
        case '+':
          result = num1 + num2;
          break;
        case '-':
          result = num1 - num2;
          break;
        case '*':
          result = num1 * num2;
          break;
        case '/':
          result = num1 / num2;
          break;
      }
      field.setText("" + result);
    }
  }
  public static void main(String[] args) {
    SimpleCalculator calculator = new SimpleCalculator();
    calculator.setVisible(true);
  }
}

在这段代码中,我们创建了一个SimpleCalculator类,该类继承自JFrame,实现了ActionListener接口。我们还定义了一个文本框field、num1、num2和result变量,以及一个操作符变量operator。在构造函数内,我们设置了窗口的标题、关闭操作、大小和布局。在GUI中,我们添加了一个文本框和16个按钮,将它们放在一个网格布局中。我们还为每个按钮添加ActionListener。

在actionPerformed方法内,我们通过getActionCommand方法获取按钮的标签。如果它是数字按钮(0到9),我们将其添加到文本框中。如果它是小数点按钮(.),我们将其添加到文本框中。如果它是操作符按钮(+,-,*,/),我们保存当前数字,设置操作符,并清空文本框。如果它是等于按钮(=),我们保存当前数字,根据操作符进行计算,并将结果设置为文本框的内容。

这个简单的计算器程序只是一个开始。我们可以添加更多的操作,例如与内存相关的操作,支持负数,添加更多的函数等等。现在,您已经学会了如何从头开始设计简单的Java应用程序,您可以根据需要进行扩展和改进。

  
  

评论区