java计算器

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame implements ActionListener {

  private JTextField textField;
  private JButton[] numberButtons = new JButton[10];
  private JButton[] functionButtons = new JButton[8];
  private JButton addButton, subButton, mulButton, divButton;
  private JButton decButton, equButton, delButton, clrButton, negButton, sqrtButton;
  private JPanel panel;

  private double num1 = 0, num2 = 0, result = 0;
  private char operator;

  Calculator() {
    this.setTitle("Calculator");
    this.setSize(420, 550);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(null);

    textField = new JTextField();
    textField.setBounds(50, 25, 300, 50);
    textField.setFont(new Font("Arial", Font.BOLD, 30));
    textField.setEditable(false);

    addButton = new JButton("+");
    subButton = new JButton("-");
    mulButton = new JButton("*");
    divButton = new JButton("/");
    decButton = new JButton(".");
    equButton = new JButton("=");
    delButton = new JButton("C");
    clrButton = new JButton("CE");
    negButton = new JButton("+/-");
    sqrtButton = new JButton("sqrt");

    functionButtons[0] = addButton;
    functionButtons[1] = subButton;
    functionButtons[2] = mulButton;
    functionButtons[3] = divButton;
    functionButtons[4] = decButton;
    functionButtons[5] = equButton;
    functionButtons[6] = delButton;
    functionButtons[7] = clrButton;

    for (JButton button : functionButtons) {
      button.setFont(new Font("Arial", Font.BOLD, 18));
      button.addActionListener(this);
    }

    for (int i = 0; i < 10; i++) {
      numberButtons[i] = new JButton(String.valueOf(i));
      numberButtons[i].setFont(new Font("Arial", Font.BOLD, 25));
      numberButtons[i].addActionListener(this);
    }

    negButton.setFont(new Font("Arial", Font.BOLD, 15));
    sqrtButton.setFont(new Font("Arial", Font.BOLD, 18));

    delButton.setBounds(50, 430, 100, 50);
    clrButton.setBounds(150, 430, 100, 50);
    negButton.setBounds(250, 430, 100, 50);

    panel = new JPanel();
    panel.setBounds(50, 100, 300, 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(subButton);
    panel.add(numberButtons[7]);
    panel.add(numberButtons[8]);
    panel.add(numberButtons[9]);
    panel.add(mulButton);
    panel.add(decButton);
    panel.add(numberButtons[0]);
    panel.add(equButton);
    panel.add(divButton);

    this.add(panel);
    this.add(negButton);
    this.add(sqrtButton);
    this.add(delButton);
    this.add(clrButton);
    this.add(textField);
    this.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() == decButton) {
      textField.setText(textField.getText().concat("."));
    }
    if (e.getSource() == addButton) {
      num1 = Double.parseDouble(textField.getText());
      operator = '+';
      textField.setText("");
    }
    if (e.getSource() == subButton) {
      num1 = Double.parseDouble(textField.getText());
      operator = '-';
      textField.setText("");
    }
    if (e.getSource() == mulButton) {
      num1 = Double.parseDouble(textField.getText());
      operator = '*';
      textField.setText("");
    }
    if (e.getSource() == divButton) {
      num1 = Double.parseDouble(textField.getText());
      operator = '/';
      textField.setText("");
    }
    if (e.getSource() == equButton) {
      num2 = Double.parseDouble(textField.getText());
      switch (operator) {
        case '+':
          result = num1 + num2;
          break;
        case '-':
          result = num1 - num2;
          break;
        case '*':
          result = num1 * num2;
          break;
        case '/':
          if (num2 == 0) {
            textField.setText("Error");
          } else {
            result = num1 / num2;
          }
          break;
      }
      textField.setText(String.valueOf(result));
      num1 = result;
    }
    if (e.getSource() == clrButton) {
      textField.setText("");
    }
    if (e.getSource() == delButton) {
      String str = textField.getText();
      textField.setText("");
      for (int i = 0; i < str.length() - 1; i++) {
        textField.setText(textField.getText() + str.charAt(i));
      }
    }
    if (e.getSource() == negButton) {
      double temp = Double.parseDouble(textField.getText());
      temp *= -1;
      textField.setText(String.valueOf(temp));
    }
    if (e.getSource() == sqrtButton) {
      double temp = Double.parseDouble(textField.getText());
      temp = Math.sqrt(temp);
      textField.setText(String.valueOf(temp));
    }
  }
}
```

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值