简易计算器的实现
java 练习项目-计算器的简单实现
实现简单的四则运算,使用swing图形组件开发。
开发思路
它由三部分组成,图形界面,界面响应逻辑,数值计算逻辑。
- 界面结构 :主体为JFrame,流式布局,中间容器为两个JPanel,一个是数字键和符号键(GridLayout布局),另一个是输出文本框和清除按钮(BorderLayout布局);
- 存储: 将每一次点击事件,记录在一个JButton Vector容器中进行复合运算;
- 事件响应逻辑 :包括加减乘除运算,清除,小数点,等于(可以以内部类的形式实现相关逻辑,并实现action接口);
- 面向对象思想:将计数器抽象成一个类,它的属性有:左右操作数,运算符号,vector数组,界面组件(按钮,文本框,窗口),以及标志位,在构造器中实例化界面及注册监听器,方法有:实现两操作数的运算;
- 其他细节 除法运算时要注意除数为零的情况;
结果展示
计算器:
代码实现
// An highlighted block
public class Calculator {
String str1 = "0";//左操作数
String str2 = "0";//右操作数
String signal = "+";//运算符
String result = "";//结果
int operand = 1;//操作数,1为str1,2为str2
boolean symbols = true;//符号标志位
//清零
boolean clear_left = true;
boolean clear_right = true;
int d = 1;
JFrame frame = new JFrame("calculator");
JTextField result_text = new JTextField(13);
JButton clear = new JButton("clear");
//计算按钮
JButton zero = new JButton("0");
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
//运算符
JButton dian = new JButton(".");
JButton jia = new JButton("+");
JButton jian = new JButton("-");
JButton cheng = new JButton("*");
JButton chu = new JButton("/");
JButton equal = new JButton("=");
//存储数组
JButton store;
Vector vt = new Vector(20, 10);//容量为20,大小为10
//构造器
public Calculator() {
result_text.setHorizontalAlignment(JTextField.RIGHT);//文本框右对齐
JPanel center = new JPanel();
center.setLayout(new GridLayout(4, 4, 10, 10));
center.add(seven);
center.add(eight);
center.add(nine);
center.add(chu);
center.add(four);
center.add(five);
center.add(six);
center.add(cheng);
center.add(one);
center.add(two);
center.add(three);
center.add(jian);
center.add(zero);
center.add(dian);
center.add(equal);
center.add(jia);
center.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel north = new JPanel();
north.setLayout(new BorderLayout());
north.add(result_text, BorderLayout.WEST);
north.add(clear, BorderLayout.EAST);
frame.setLocation(600, 200);
frame.setResizable(false);
frame.setLayout(new FlowLayout());
frame.add(north);
frame.add(center);
frame.setSize(250, 230);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//数字键
class Listener_normal implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String s = ((JButton) e.getSource()).getText();
store = (JButton) e.getSource();
vt.add(store);
if (operand == 1) {//写入哪一个字符串
if (clear_left) {//是否清空
str1 = "";
d = 1;
}
str1 = str1 + s;
clear_left = false;
result_text.setText(str1);
} else if (operand == 2) {
if (clear_right) {
str2 = "";
d = 1;
}
str2 = str2 + s;
clear_right = false;
result_text.setText(str2);
}
}
}
//符号键
class Listener_signal implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String s = ((JButton) e.getSource()).getText();
store = (JButton) e.getSource();
vt.add(store);
if (symbols) {
operand = 2;
d = 1;
signal = s;
symbols = false;
} else {
int a = vt.size();
JButton c = (JButton) vt.get(a - 2);
if (!(c.getText().equals("+")) && !(c.getText().equals("-")) && !(c.getText().equals("*")) && !(c.getText().equals("/"))) {
cal();
str1 = result;
// 开关 k1 为 1 时,向数 1 写值,为2时向数2写
operand = 2;
d = 1;
clear_right = true;
signal = s;
}
symbols = false;
}
}
}
//清除键
class Listener_clear implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
store = (JButton) e.getSource();
vt.add(store);
d = 1;
symbols = true;
operand = 1;
clear_left = true;
clear_right = true;
str1 = "0";
str2 = "0";
signal = "";
result = "";
result_text.setText(result);
vt.clear();
}
}
//等于
class Listener_equal implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
store = (JButton) e.getSource();
vt.add(store);
cal();
operand = 1;
symbols = true;
clear_left = true;
clear_right = true;
str1 = result;
}
}
class Listener_dian implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
store = (JButton) e.getSource();
vt.add(store);
if (d == 1) {
String s = ((JButton) e.getSource()).getText();
if (operand == 1) {
if (clear_left) {
str1 = "";
d = 1;
}
str1 = str1 + s;
clear_left = false;
result_text.setText(str1);
} else if (operand == 2) {
if (clear_right) {
str2 = "";
d = 1;
}
str2 = str2 + s;
clear_right = false;
result_text.setText(str2);
}
}
d = d + 1;
}
}
Listener_equal eq = new Listener_equal();
Listener_normal L = new Listener_normal();
Listener_signal sig = new Listener_signal();
Listener_clear cle = new Listener_clear();
Listener_dian di = new Listener_dian();
seven.addActionListener(L);
eight.addActionListener(L);
nine.addActionListener(L);
chu.addActionListener(sig);
four.addActionListener(L);
five.addActionListener(L);
six.addActionListener(L);
cheng.addActionListener(sig);
one.addActionListener(L);
two.addActionListener(L);
three.addActionListener(L);
jian.addActionListener(sig);
zero.addActionListener(L);
dian.addActionListener(di);
equal.addActionListener(eq);
jia.addActionListener(sig);
clear.addActionListener(cle);
}
public void cal(){
double a,b;
String c = signal;
double results = 0;
if (c.equals("")){
result_text.setText("please input operator");
}else{
if (str1.equals("."))
str1 = "0.0";
if (str2.equals("."))
str2 = "0.0";
a = Double.valueOf(str1).doubleValue();
b = Double.valueOf(str2).doubleValue();
if (c.equals("+")){
results = a + b;
}
if (c.equals("-")){
results = a - b;
}
if (c.equals("*")){
BigDecimal m1 = new BigDecimal(Double.toString(a));
BigDecimal m2 = new BigDecimal(Double.toString(b));
results = m1.multiply(m2).doubleValue();
}
if (c.equals("/")){
if (b == 0){
results = 0;
}else {
results = a / b;
}
}
result = (new Double(results).toString());
result_text.setText(result);
}
}
public static void main(String[] args) {
Calculator cal = new Calculator();
}
}