这两天学到swing,于是做了个计算器,能够满足基本的计算要求,全当练手的。
其实这个看起来简单,实际实现起来还是有很多药注意的;比如字符串的处理以及一些特殊情况的处理,一下是实现代码:
package com.itany.day01.calculator;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class calculator extends JFrame implements ActionListener
{
private JFrame jf = new JFrame("计算器");
private JTextArea text = new JTextArea();
private StringBuffer sb = new StringBuffer();//用来存数字的内容
private String temp = "=";//用来存放运算符
float result = 0;
private JPanel p1 = new JPanel();
private JButton b1 = new JButton("1");
private JButton b2 = new JButton("2");
private JButton b3 = new JButton("3");
private JButton b4 = new JButton("+");
private JButton b5 = new JButton("4");
private JButton b6 = new JButton("5");
private JButton b7 = new JButton("6");
private JButton b8 = new JButton("-");
private JButton b9 = new JButton("7");
private JButton b10 = new JButton("8");
private JButton b11 = new JButton("9");
private JButton b12 = new JButton("*");
private JButton b13 = new JButton("0");
private JButton b14 = new JButton("c");
private JButton b15 = new JButton("/");
private JButton b16 = new JButton("=");
public calculator()
{
Container con = this.getContentPane();
// 北部
con.add(text, BorderLayout.NORTH);
// 中部
p1.setLayout(new GridLayout(4, 4));
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b10);
p1.add(b11);
p1.add(b12);
p1.add(b13);
p1.add(b14);
p1.add(b15);
p1.add(b16);
con.add(p1, BorderLayout.CENTER);
//监听器
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
this.setSize(400, 400);
this.setLocation(500, 250);
this.setVisible(true);
this.setDefaultCloseOperation(3);
}
public static void main(String[] args)
{
new calculator();
}
public void actionPerformed(ActionEvent e)
{
String name = e.getActionCommand();
if("1".equals(name)){
sb.append(name);
text.setText(sb+"");
return;
}
if("2".equals(name)){
sb.append(name);
text.setText(sb+"");
return;
}
if("3".equals(name)){
sb.append(name);
text.setText(sb+"");
return;
}
if("4".equals(name)){
sb.append(name);
text.setText(sb+"");
return;
}
if("5".equals(name)){
sb.append(name);
text.setText(sb+"");
return;
}
if("6".equals(name)){
sb.append(name);
text.setText(sb+"");
return;
}
if("7".equals(name)){
sb.append(name);
text.setText(sb+"");
return;
}
if("8".equals(name)){
sb.append(name);
text.setText(sb+"");
return;
}
if("9".equals(name)){
sb.append(name);
text.setText(sb+"");
return;
}
if("0".equals(name)){
sb.append(name);
text.setText(sb+"");
return;
}
if("+".equals(name)){
result = Float.parseFloat(sb+"");//符号前面的数
sb.delete(0,sb.length());//清空前面的数字
temp = "+";
text.setText("+");
return;
}
if("-".equals(name)){
result = Float.parseFloat(sb+"");//符号前面的数
sb.delete(0,sb.length());//清空前面的数字
temp = "-";
text.setText("-");
return;
}
if("*".equals(name)){
result = Float.parseFloat(sb+"");//符号前面的数
sb.delete(0,sb.length());//清空前面的数字
temp = "*";
text.setText("*");
return;
}
if("/".equals(name)){
result = Float.parseFloat(sb+"");//符号前面的数
sb.delete(0,sb.length());//清空前面的数字
temp = "/";
text.setText("/");
return;
}
if("c".equals(name)){
result = 0;
sb.delete(0, sb.length());
text.setText("");
return;
}
if("=".equals(name)){
if("+".equals(temp)){
result += (float)Float.parseFloat(sb+"");
temp = "=";
}
else if("-".equals(temp)){
result -= (float)Float.parseFloat(sb+"");
temp = "=";
}
else if("*".equals(temp)){
result *= (float)Float.parseFloat(sb+"");
temp = "=";
}
else if("/".equals(temp)){
if((float)Float.parseFloat(sb+"")!=0){
result /= (float)Float.parseFloat(sb+"");
temp = "=";
}else{
result = 0;
sb.delete(0, sb.length());
text.setText("错误");
return;
}
}
else if("=".equals(temp)){
text.setText(Float.parseFloat(sb+"")+"");
return;
}
text.setText(result+"");
sb.delete(0, sb.length());
sb.append(result);
return;
}
}
}