Calculator

目录

Student’s information

PSP form

Description of problem-solving ideas

Design and implementation

Code description

Display

Summary


Student’s information

The Link Your Classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/617332156
The Aim of This AssignmentCreate a calculator with a visual interface.
MU STU ID and FZU STU ID21126399/832101315

GitHub - Cyrene56/calculator

PSP form

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning 
• Estimate4540
Development
• Analysis4545
• Design Spec3030
• Design Review1515
• Coding Standard3035
• Design4550
• Coding100120
• Code Review3035
• Test2535
Reporting
• Test Report3040
• Size Measurement1515
• Postmortem & Process Improvement Plan3035
Sum 440495

Description of problem-solving ideas

Create a calculator interface by setting up a JFrame as the main window. Add a JTextField to display user input and results. Place a set of JButtons on a JPanel, arranging them neatly with a grid layout. Attach MouseListeners to respond to button clicks, performing actions based on the button text, such as appending numbers or operators. When the "=" button is clicked, parse the expression, execute the math operation, and display the result. Include error handling to prevent division by zero and allow character deletion in the text field. All interactions occur within the text field, enabling users to input and retrieve results by clicking buttons.

Design and implementation

Code description

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

public class Calculator {
    public JFrame frame;           
    public JTextField textField;    
    public JButton[] buttons;       
    public JPanel panel;           
    public String[] buttonArr = {"%", "DEL", "C", "+", "7", "8", "9", "-", "4", "5", "6", "×", "1", "2", "3", "÷", "+/-", "0", ".", "="};
    public String information = "";  
    public String a, b, operate;      
    public double result;            

    public void init() {
        frame = new JFrame("简易计算器");  
        textField = new JTextField();    
        panel = new JPanel();              
        buttons = new JButton[buttonArr.length];

        frame.setBounds(700, 188, 490, 580);  
        textField.setBounds(10, 15, 465, 90);  
        textField.setFont(new Font("宋体", Font.BOLD, 60));
        textField.setBackground(new Color(119, 136, 153));

        panel.setBackground(new Color(135, 206, 250));
        panel.setBounds(10, 130, 465, 400);  
        panel.setLayout(new GridLayout(5, 4, 20, 20));    

        frame.add(textField);                               
        frame.add(panel);                                   

        for (int i = 0; i < buttonArr.length; i++) {
            int index = i;
            buttons[i] = new JButton(buttonArr[i]);
            panel.add(buttons[i]);
            buttons[i].setFont(new Font("宋体", Font.BOLD, 35));
            buttons[i].setBackground(new Color(135, 206, 235));

            buttons[i].addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    input(buttons[index].getText());
                }
            });
        }

        frame.setResizable(false);
        frame.setLayout(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void input(String v) {
        switch (v) {
            case "1": case "2": case "3": case "4": case "5":
            case "6": case "7": case "8": case "9": case "0":
                information += v;
                textField.setText(information);
                break;
            case ".":
                information += ".";
                textField.setText(information);
                break;
            case "C":
                information = "";
                textField.setText(information);
                break;
            case "DEL":
                if (!information.isEmpty()) {
                    information = information.substring(0, information.length() - 1);
                    textField.setText(information);
                }
                break;
            case "+/-":
                if (!information.isEmpty()) {
                    double num = Double.parseDouble(information);
                    num = -num;
                    information = String.valueOf(num);
                    textField.setText(information);
                }
                break;
            case "%":
                if (!information.isEmpty()) {
                    double num = Double.parseDouble(information);
                    num /= 100;
                    information = String.valueOf(num);
                    textField.setText(information);
                }
                break;
            case "+": case "-": case "×": case "÷":
                information += v;
                operate = v;
                textField.setText(information);
                break;
            case "=":
                if (!information.isEmpty() && information.contains(operate)) {
                    String[] operands = information.split(operate);
                    a = operands[0];
                    b = operands[1];
                    result();
                    textField.setText(information);
                }
                break;
        }
    }

    public void result() {
        switch (operate) {
            case "+":
                result = Double.parseDouble(a) + Double.parseDouble(b);
                information = String.valueOf(result);
                break;
            case "-":
                result = Double.parseDouble(a) - Double.parseDouble(b);
                information = String.valueOf(result);
                break;
            case "×":
                result = Double.parseDouble(a) * Double.parseDouble(b);
                information = String.valueOf(result);
                break;
            case "÷":
                if (!b.equals("0")) {
                    result = Double.parseDouble(a) / Double.parseDouble(b);
                    information = String.valueOf(result);
                } else {
                    information = "False"; // 
                }
                break;
        }
    }

    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        calculator.init();
    }
}

Display

Summary

Through this assignment, I have learned how to create a basic calculator application. Key steps include creating the user interface, button layout, button event handling, expression evaluation, and error handling. This experience has been valuable in understanding the development process of GUI applications and how to design user-friendly interfaces while managing user input. I have gained a lot from this.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值