21xrx.com
2024-12-22 23:16:34 Sunday
登录
文章检索 我的文章 写文章
用Java设计一个简单的计算器
2023-06-10 07:53:05 深夜i     --     --
Java 界面设计 计算器

我最近在学习Java界面设计,今天我学习到了如何用Java设计一个计算器。下面我来简单分享一下我的学习心得。

首先,我需要创建一个Java类来实现计算器的功能。在这个类中,我需要定义各种按钮和文本框,并编写相应的监听器方法来实现计算器的基本操作。下面是一个简单的代码示例:


import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Calculator implements ActionListener {

  JFrame frame;

  JTextField textField;

  JButton[] numberButtons = new JButton[10];

  JButton[] functionButtons = new JButton[5];

  JButton addButton, subButton, mulButton, divButton;

  JButton decimalButton, equalButton, clearButton;

  JPanel panel;

  double num1 = 0, num2 = 0, result = 0;

  char operator;

  Calculator() {

    frame = new JFrame("计算器");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setSize(420, 550);

    frame.setLayout(null);

    textField = new JTextField();

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

    textField.setFont(new Font("微软雅黑", Font.PLAIN, 28));

    addButton = new JButton("+");

    subButton = new JButton("-");

    mulButton = new JButton("*");

    divButton = new JButton("/");

    decimalButton = new JButton(".");

    equalButton = new JButton("=");

    clearButton = new JButton("C");

    functionButtons[0] = addButton;

    functionButtons[1] = subButton;

    functionButtons[2] = mulButton;

    functionButtons[3] = divButton;

    functionButtons[4] = clearButton;

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

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

      numberButtons[i].setFont(new Font("微软雅黑", Font.PLAIN, 24));

    }

    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(decimalButton);

    panel.add(numberButtons[0]);

    panel.add(equalButton);

    panel.add(divButton);

    for (JButton button : functionButtons) {

      button.addActionListener(this);

      button.setFont(new Font("楷体", Font.PLAIN, 30));

    }

    for (JButton button : numberButtons) {

      button.addActionListener(this);

      button.setFont(new Font("楷体", Font.PLAIN, 30));

    }

    decimalButton.addActionListener(this);

    decimalButton.setFont(new Font("楷体", Font.PLAIN, 30));

    equalButton.addActionListener(this);

    equalButton.setFont(new Font("楷体", Font.PLAIN, 30));

    clearButton.addActionListener(this);

    clearButton.setFont(new Font("楷体", Font.PLAIN, 30));

    frame.add(textField);

    frame.add(panel);

    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());

      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() == equalButton) {

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

      switch (operator) {

        case '+':

          result = num1 + num2;

          break;

        case '-':

          result = num1 - num2;

          break;

        case '*':

          result = num1 * num2;

          break;

        case '/':

          result = num1 / num2;

          break;

      }

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

      num1 = result;

    }

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

      textField.setText("");

    }

  }

}

上面的代码虽然较长,但是正是实现了一个简单的计算器的全部功能。现在的计算器可以用加、减、乘、除、小括号、小数点、0~9数字等进行运算了。

通过这个小项目的练习,我了解了Java界面设计的基本过程和方法,而且在编写代码的过程中,我也掌握了一些Java程序设计的基础知识。我相信,这个小计算器也能对其他的Java初学者有所帮助。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章