//import javax.swing.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class counter extends Applet implements ActionListener{
public TextField text;
public Button zero,one,two,three,four,five,
six,seven,eight,nine,plus,sub,mul,div,equal,ce,point,tzero;
public void init(){
text=new TextField("0");
text.setEditable(false);//不能编辑
zero = new Button("0");//数字键0
one = new Button("1");//数字键1
two = new Button("2");//数字键2
three = new Button("3");//数字键3
four = new Button("4");//数字键4
five = new Button("5");//数字键5
six = new Button("6");//数字键6
seven = new Button("7");//数字键7
eight = new Button("8");//数字键8
nine = new Button("9");//数字键9
plus = new Button("+");
sub = new Button("-");
mul = new Button("*");
div = new Button("/");
equal = new Button("=");
ce = new Button("ce");//置零键
point = new Button(".");
tzero = new Button("00");
one.addActionListener(this);//对1按钮添加监听事件
two.addActionListener(this);//对2按钮添加监听事件
three.addActionListener(this);//对3按钮添加监听事件
four.addActionListener(this);//对4按钮添加监听事件
five.addActionListener(this);//对5按钮添加监听事件
six.addActionListener(this);//对6按钮添加监听事件
seven.addActionListener(this);//对7按钮添加监听事件
eight.addActionListener(this);//对8按钮添加监听事件
nine.addActionListener(this);//对9按钮添加监听事件
zero.addActionListener(this);//对0按钮添加监听事件
ce.addActionListener(this);//对置零按钮添加监听事件
plus.addActionListener(this);//对+按钮添加监听事件
equal.addActionListener(this);//对=按钮添加监听事件
sub.addActionListener(this);//对-按钮添加监听事件
mul.addActionListener(this);//对*按钮添加监听事件
div.addActionListener(this);//对/按钮添加监听事件
tzero.addActionListener(this);//对00按钮添加监听事件
point.addActionListener(this);//对.按钮添加监听事件
Panel p1=new Panel(new BorderLayout());
p1.add("Center",text);
Panel p2=new Panel(new GridLayout(6,3,20,10));
p2.add(seven);
p2.add(eight);
p2.add(nine);
p2.add(four);
p2.add(five);
p2.add(six);
p2.add(one);
p2.add(two);
p2.add(three);
p2.add(zero);
p2.add(tzero);
p2.add(plus);
p2.add(sub);
p2.add(mul);
p2.add(div);
p2.add(point);
p2.add(equal);
p2.add(ce);
Panel p=new Panel(new BorderLayout());
p.add("Center",p1);
p.add("South",p2);
add(p);
}
public void actionPerformed(ActionEvent e){
boolean com = false;
//当i=0时说明是我们第一次输入,字符串sum不会累加
int i = 0;
//存放text的内容
String sum = "";
//存放点击按钮+、-、*、/之前的数值
double total = 0;
//+、-、*、/的代号分别为1,2,3,4
int symbol = 0;
if (e.getActionCommand().equals("1")) {
if (com || i == 0) {
text.setText("1");
com = false;
i = 1;
} else {
sum = text.getText();
text.setText(sum + "1");
}
} else if (e.getActionCommand().equals("2")) {
if (com || i == 0) {
text.setText("2");
com = false;
i = 1;
} else {
sum = text.getText();
text.setText(sum + "2");
}
} else if (e.getActionCommand().equals("3")) {
if (com || i == 0) {
text.setText("3");
com = false;
i = 1;
} else {
sum = text.getText();
text.setText(sum + "3");
}
} else if (e.getSource() == four) {
if (com || i == 0) {
text.setText("4");
com = false;
i = 1;
} else {
sum = text.getText();
text.setText(sum + "4");
}
} else if (e.getSource() == five) {
if (com || i == 0) {
text.setText("5");
com = false;
i = 1;
} else {
sum = text.getText();
text.setText(sum + "5");
}
} else if (e.getSource() == six) {
if (com || i == 0) {
text.setText("6");
com = false;
i = 1;
} else {
sum = text.getText();
text.setText(sum + "6");
}
} else if (e.getSource() == seven) {
if (com || i == 0) {
text.setText("7");
com = false;
i = 1;
} else {
sum = text.getText();
text.setText(sum + "7");
}
} else if (e.getSource() == eight) {
if (com || i == 0) {
text.setText("8");
com = false;
i = 1;
} else {
sum = text.getText();
text.setText(sum + "8");
}
} else if (e.getSource() == nine) {
if (com || i == 0) {
text.setText("9");
com = false;
i = 1;
} else {
sum = text.getText();
text.setText(sum + "9");
}
}
else if (e.getSource() == zero) {
if (com || i == 0) {
text.setText("0");
com = false;
i = 1;
} else {
sum = text.getText();
if (Float.parseFloat(sum) > 0 || Float.parseFloat(sum) < 0) {
text.setText(sum + "0");
} else {
if (sum.trim().indexOf(".") == -1) {
text.setText(sum);
} else {
text.setText(sum + "0");
}
}
}
} else if (e.getSource() == ce) {
text.setText("0");
i = 0;
com = true;
} else if (e.getSource() == tzero) {
if (com || i == 0) {
text.setText("0");
com = false;
i = 1;
} else {
sum = text.getText();
if (Float.parseFloat(sum) > 0 || Float.parseFloat(sum) < 0) {
text.setText(sum + "00");
} else {
if (sum.trim().indexOf(".") == -1) {
text.setText(sum);
} else {
text.setText(sum + "00");
}
}
}
}
else if (e.getSource() == point) {
if (com || i == 0) {
text.setText("0.");
com = false;
i = 1;
} else {
sum = text.getText();
if (sum.trim().indexOf(".") == -1) {
text.setText(sum + ".");
} else {
text.setText(sum);
}
}
}//获得点击+之前的数值
else if (e.getSource() == plus) {
com = true;
i = 0;
total = Double.parseDouble(text.getText());
symbol = 1;
}//获得点击-之前的数值
else if (e.getSource() == sub) {
com = true;
i = 0;
total = Double.parseDouble(text.getText());
symbol = 2;
}//获得点击*之前的数值
else if (e.getSource() == mul) {
com = true;
i = 0;
total = Double.parseDouble(text.getText());
System.out.println(total);
symbol = 3;
}//获得点击/之前的数值
else if (e.getSource() == div) {
com = true;
i = 0;
total = Double.parseDouble(text.getText());
symbol = 4;
}else if (e.getSource() == equal){
total=Double.parseDouble(text.getText());
suanfa a=new suanfa();
Double b=a.sum(sum,total,symbol);
String v=Double.toString(b);
text.setText(v);
i=0;
com=false;
}
}
}
这个是主类!下面是调用的一个类
class suanfa{
double sum(String sum,double total,int symbol){
double ad=0;
switch (symbol) {
case 1 ://计算加法
{
ad =total + Double.parseDouble(sum);
break;
}
case 2 ://计算减法
{
ad =total - Double.parseDouble(sum);
break;
}
case 3 ://计算乘法
{
ad =total * Double.parseDouble(sum);
break;
}
case 4 ://计算除法
{
ad =total / Double.parseDouble(sum);
break;
}
}
return ad;
}
}
帮忙看一下我的程序怎么就不能实现累加呀,别的都可以了!