176
社区成员




This blog will show how I used the java language to design a calculator program which can realize add, subtract, multiply, divide, and make zero functions. I will introduce some of my personal design ideas and implementation process as well as code description, and at the end of the blog I will show a demonstration video of the calculation results.
The Link Your Class | https://bbs.csdn.net/forums/ssynkqtd-04?typeId=5171415 |
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/617332156 |
The Aim of This Assignment | build a calculator |
MU STU ID and FZU STU ID | 2414719858 || 832102201 |
Personal Software Process Stages | Estimated Time(minutes) | Actual Time(minutes) |
Planning | ||
Estimate | ||
Development | ||
Analysis | ||
Design Spec | ||
Design Review | ||
Coding Standard | ||
Design | ||
Coding | ||
Code Review | ||
Test | ||
Reporting | ||
Test Repor | ||
Size Measurement | ||
Postmortem & Process Improvement Plan | ||
Sum |
We need to design a calculator and implement simple addition, subtraction, multiplication and division. I think these functions can be done by compiler.
The Programming Language is java and the compiler I used is eclipse.
And we need use this header file import javax.swing.*; (Swing provides how to interact with the calculator.) We also need to use Inheritance, whitch can help with the implementation of the calculation logic.
Because we need to create a visual interface,we need use .setTitle() / .setSize() / .setLayout() / this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)..... and so on functions to set this interface.
we also need use .add() to add controls, such as TEXT or BUTTONG
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.*;
Swing provides how to interact with the calculator.
In this code I created a window called "Caculator" and set the state of various properties of the window.
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) can end the application which the window is in.
//**********************初始化窗口************************
public void init() {
this.setTitle("Caculator");// 命名窗口
this.setSize(FRAME_W, FRAME_H);// 窗口大小
this.setLayout(new BorderLayout());
this.setResizable(false);// 设置窗口不可拉伸
this.setLocation(frame_x, frame_y);// 窗口处于中心位置
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置结束窗口所在的应用程序
}
//*********************显示窗口****************************
public static void main(String[] args) {
Caculator caculator = new Caculator();
caculator.setVisible(true);// setVisible(boolean)方法是用来显示/隐藏窗口。
}
In this code, you add buttons to the window and a text field to display.
//***********************添加北面控件***********************
public void addNorth() {
// 设置文本框大小
this.input_text.setPreferredSize(new Dimension(400, 65));
jp_north.add(input_text);// 在jp_north面板中添加文本框
// 设置归零按钮大小
this.c_bt.setPreferredSize(new Dimension(75, 75));// 设置button大小
c_bt.setFont(new Font("粗体", Font.BOLD, 20));
jp_north.add(c_bt);// 在jp_north面板中添加button
c_bt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
input_text.setText(null);
}
});
this.add(jp_north, BorderLayout.NORTH);// 添加jp_north版面在窗口北面
}
//********************设置中间按钮**************************
public void addCenterButton() {
String bt = "789/456*123+.0=-";
this.jp_center.setLayout(new GridLayout(4, 4));// 设为4*4的按钮格
for (int i = 0; i < 16; i++) {
String temp = bt.substring(i, i + 1);
JButton btn = new JButton();
btn.setText(temp);
if (temp.equals("+") || temp.equals("-") || temp.equals("*") || temp.equals("/") || temp.equals(".")
|| temp.equals("=")) {
btn.setFont(new Font("粗体", Font.BOLD, 30));
} else {
btn.setFont(new Font("粗体", Font.ITALIC, 20));
}
btn.addActionListener(this);
jp_center.add(btn);
}
this.add(jp_center, BorderLayout.CENTER);// 添加jp_center版面在窗口中间
}
//********************获取按钮文字************************
public void actionPerformed(ActionEvent e) {
String clickStr = e.getActionCommand();
if (".0123456789".indexOf(clickStr) != -1) { // 若输入这些符号则识别为数字
this.input_text.setText(input_text.getText() + clickStr);
this.input_text.setHorizontalAlignment(JTextField.RIGHT);// 将文字向右对齐
this.input_text.setFont(new Font("粗体", Font.ITALIC, 20));// 设置字体以及大小
} else if ("+-*/".indexOf(clickStr) != -1) {
operator = clickStr;
firstInput = this.input_text.getText();// 记录第一次输入的值
this.input_text.setText(null);// 清空文本框
Double a = Double.valueOf(firstInput);
Double b = Double.valueOf(this.input_text.getText());
Double result = a + b;
} else if (clickStr.equals("=")) {
Double a = Double.valueOf(firstInput);
Double b = Double.valueOf(this.input_text.getText());
Double result = null;
switch (operator) {
case "+":
result = a + b;
break;
case "-":
result = a - b;
break;
case "*":
result = a * b;
break;
case "/":
if (b != 0) {
result = a / b;
}
break;
}
this.input_text.setText(result.toString());
}
}
}
package chinaNB;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.*;
public class Caculator extends JFrame implements ActionListener {// implements ActionListener监听按钮
// ******************常数设置******************************
// 设置应用窗口的大小
public static final int FRAME_W = 500;
public static final int FRAME_H = 500;
// 得到屏幕的宽度和高度
public static final int SCREEN_W = Toolkit.getDefaultToolkit().getScreenSize().width;
public static final int SCREEN_H = Toolkit.getDefaultToolkit().getScreenSize().height;
// 得到屏幕中心点位置
public int frame_x = (SCREEN_W - FRAME_W) / 2;
public int frame_y = (SCREEN_H - FRAME_H) / 2;
// 设置第一次输入和操作符
private String firstInput;
private String operator;
//********************控件设置*****************************
// 设置北面控件
private JPanel jp_north = new JPanel();// 设置面板
private JTextField input_text = new JTextField();// 设置文本框
private JButton c_bt = new JButton("C");// 设置归零按钮
// 设置中间控件
private JPanel jp_center = new JPanel();// 设置面板
//**********************调用函数***************************
public Caculator() throws HeadlessException {
this.init();
this.addNorth();
this.addCenterButton();
}
//**********************初始化窗口************************
public void init() {
this.setTitle("Caculator");// 命名窗口
this.setSize(FRAME_W, FRAME_H);// 窗口大小
this.setLayout(new BorderLayout());
this.setResizable(false);// 设置窗口不可拉伸
this.setLocation(frame_x, frame_y);// 窗口处于中心位置
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置结束窗口所在的应用程序
}
//***********************添加北面控件***********************
public void addNorth() {
// 设置文本框大小
this.input_text.setPreferredSize(new Dimension(400, 65));
jp_north.add(input_text);// 在jp_north面板中添加文本框
// 设置归零按钮大小
this.c_bt.setPreferredSize(new Dimension(75, 75));// 设置button大小
c_bt.setFont(new Font("粗体", Font.BOLD, 20));
jp_north.add(c_bt);// 在jp_north面板中添加button
c_bt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
input_text.setText(null);
}
});
this.add(jp_north, BorderLayout.NORTH);// 添加jp_north版面在窗口北面
}
//********************设置中间按钮**************************
public void addCenterButton() {
String bt = "789/456*123+.0=-";
this.jp_center.setLayout(new GridLayout(4, 4));// 设为4*4的按钮格
for (int i = 0; i < 16; i++) {
String temp = bt.substring(i, i + 1);
JButton btn = new JButton();
btn.setText(temp);
if (temp.equals("+") || temp.equals("-") || temp.equals("*") || temp.equals("/") || temp.equals(".")
|| temp.equals("=")) {
btn.setFont(new Font("粗体", Font.BOLD, 30));
} else {
btn.setFont(new Font("粗体", Font.ITALIC, 20));
}
btn.addActionListener(this);
jp_center.add(btn);
}
this.add(jp_center, BorderLayout.CENTER);// 添加jp_center版面在窗口中间
}
//*********************显示窗口****************************
public static void main(String[] args) {
Caculator caculator = new Caculator();
caculator.setVisible(true);// setVisible(boolean)方法是用来显示/隐藏窗口。
}
//********************获取按钮文字************************
public void actionPerformed(ActionEvent e) {
String clickStr = e.getActionCommand();
if (".0123456789".indexOf(clickStr) != -1) { // 若输入这些符号则识别为数字
this.input_text.setText(input_text.getText() + clickStr);
this.input_text.setHorizontalAlignment(JTextField.RIGHT);// 将文字向右对齐
this.input_text.setFont(new Font("粗体", Font.ITALIC, 20));// 设置字体以及大小
} else if ("+-*/".indexOf(clickStr) != -1) {
operator = clickStr;
firstInput = this.input_text.getText();// 记录第一次输入的值
this.input_text.setText(null);// 清空文本框
Double a = Double.valueOf(firstInput);
Double b = Double.valueOf(this.input_text.getText());
Double result = a + b;
} else if (clickStr.equals("=")) {
Double a = Double.valueOf(firstInput);
Double b = Double.valueOf(this.input_text.getText());
Double result = null;
switch (operator) {
case "+":
result = a + b;
break;
case "-":
result = a - b;
break;
case "*":
result = a * b;
break;
case "/":
if (b != 0) {
result = a / b;
}
break;
}
this.input_text.setText(result.toString());
}
}
}