目录
Description of problem-solving ideas
Student’s information
The Link Your Class | https://bbs.csdn.net/forums/ssynkqtd-04 |
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/617332156 |
The Aim of This Assignment | Create a calculator with a visual interface. |
MU STU ID and FZU STU ID | 21126399/832101315 |
Github Link
PSP form
Personal Software Process Stages | Estimated Time(minutes) | Actual Time(minutes) |
Planning | ||
• Estimate | 45 | 40 |
Development | ||
• Analysis | 45 | 45 |
• Design Spec | 30 | 30 |
• Design Review | 15 | 15 |
• Coding Standard | 30 | 35 |
• Design | 45 | 50 |
• Coding | 100 | 120 |
• Code Review | 30 | 35 |
• Test | 25 | 35 |
Reporting | ||
• Test Report | 30 | 40 |
• Size Measurement | 15 | 15 |
• Postmortem & Process Improvement Plan | 30 | 35 |
Sum | 440 | 495 |
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.