21xrx.com
2025-03-23 09:29:06 Sunday
文章检索 我的文章 写文章
Java计算器代码简易教程及实现方法
2023-06-14 21:42:21 深夜i     16     0
Java语言 计算器 Swing类库

在编程学习过程中,经常需要用到计算器来帮助解决简单的数学问题。本文将介绍如何用Java语言实现一个简单的计算器代码,并附上详细的教程和代码实现方法。

1. 实现方法

首先,我们需要明确计算器要实现的功能,包括加、减、乘、除和等于。利用Java语言,我们可以使用switch结构实现。

接下来,需要创建一个窗口并添加按钮和事件监听器。这个窗口可以使用Java中的Swing类库来实现。

然后,需要编写事件处理函数来响应按钮的单击事件。在事件处理函数中,我们可以使用Java的数学类库来实现计算机的计算功能。

最后,需要将计算所得的结果显示在窗口中。这也可以通过Swing类库的组件来实现。

2. 代码实现

下面是一个Java计算器的完整代码实现,其中包括按钮、事件监听器、计算函数和结果显示等。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame implements ActionListener {
  private static final long serialVersionUID = 1L;
  private JButton[] numbutton = new JButton[10];
  private JButton add, sub, mul, div, equal, dot, clear;
  private JTextField display;
  private JPanel panel1, panel2;
  private String num1, num2;
  private double result = 0;
  private String operator;
  public static void main(String[] args) {
    new Calculator();
  }
  public Calculator() {
    setTitle("简易Java计算器");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setSize(300, 300);
    panel1 = new JPanel();
    panel2 = new JPanel();
    display = new JTextField(18);
    display.setEditable(false);
    display.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
    numbutton[0] = new JButton("0");
    for (int i = 1; i < 10; i++)
      numbutton[i] = new JButton("" + i);
    add = new JButton("+");
    sub = new JButton("-");
    mul = new JButton("*");
    div = new JButton("/");
    equal = new JButton("=");
    dot = new JButton(".");
    clear = new JButton("AC");
    panel1.add(display);
    for (int i = 0; i < 10; i++)
      panel1.add(numbutton[i]);
    panel1.add(add);
    panel1.add(sub);
    panel1.add(mul);
    panel1.add(div);
    panel1.add(dot);
    panel1.add(equal);
    panel1.add(clear);
    add(panel1, BorderLayout.CENTER);
    for (int i = 0; i < 10; i++)
      numbutton[i].addActionListener(this);
    add.addActionListener(this);
    sub.addActionListener(this);
    mul.addActionListener(this);
    div.addActionListener(this);
    dot.addActionListener(this);
    equal.addActionListener(this);
    clear.addActionListener(this);
    add(panel2, BorderLayout.SOUTH);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    for (int i = 0; i < 10; i++) {
      if (e.getSource() == numbutton[i]) {
        if (display.getText().equals("0"))
          display.setText("" + i);
        else
          display.setText(display.getText() + i);
      }
    }
    if (e.getSource() == add) {
      num1 = display.getText();
      operator = "+";
      display.setText("0");
    }
    if (e.getSource() == sub) {
      num1 = display.getText();
      operator = "-";
      display.setText("0");
    }
    if (e.getSource() == mul) {
      num1 = display.getText();
      operator = "*";
      display.setText("0");
    }
    if (e.getSource() == div) {
      num1 = display.getText();
      operator = "/";
      display.setText("0");
    }
    if (e.getSource() == dot) {
      if (display.getText().indexOf(".") == -1)
        display.setText(display.getText() + ".");
    }
    if (e.getSource() == clear) {
      display.setText("0");
    }
    if (e.getSource() == equal) {
      num2 = display.getText();
      double d1 = Double.parseDouble(num1);
      double d2 = Double.parseDouble(num2);
      if (operator.equals("+"))
        result = d1 + d2;
      if (operator.equals("-"))
        result = d1 - d2;
      if (operator.equals("*"))
        result = d1 * d2;
      if (operator.equals("/"))
        result = d1 / d2;
      display.setText("" + result);
    }
  }
}

3. 关键词

Java语言、计算器、Swing类库。

  
  

评论区

请求出错了