日期:2014-05-20  浏览次数:20736 次

花几分钟帮忙看下新手计算器代码问题

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;

import static java.awt.BorderLayout.*;
public class jisuan{



  static TextField textField= new TextField(30);

  static Button[] b = new Button[18];
public static void main(String[] args)
{ Frame f=new Frame("计算器");
  Panel p1=new Panel();

   
p1.add(textField);
f.add(p1 , NORTH);
Panel p2=new Panel();

p2.setLayout(new GridLayout(3,5,4,4));
String[] name=
{"0","1","2","3","4","5","6","7","8","9","+","-","*","/",".","="
}; // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
li f1=new li();
for(int i=0;i<name.length;i++)
{  
b[i]=new Button(name[i]);
p2.add(b[i]);
}


for(int i=0;i<name.length;i++)
{  
 
b[i].addActionListener(f1);
}


f.add(p2);
f.pack();
f.setVisible(true);



class li implements ActionListener{
public void actionPerformed(ActionEvent e) {

String value="0";
StringBuffer temp = new StringBuffer("");
String optValue = "0";
String optType="";
boolean isChoiseOptType=true;

if (e.getSource().equals(b[0])) {
temp.append(b[0].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[1])) {
temp.append(b[1].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[2])) {
temp.append(b[2].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[3])) {
temp.append(b[3].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[4])) {
temp.append(b[4].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[5])) {
temp.append(b[5].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[6])) {
temp.append(b[6].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[7])) {
temp.append(b[7].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[8])) {
temp.append(b[8].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[9])) {
temp.append(b[9].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[14])) {
if(temp.length()<=0)
temp.append("0");
temp.append(b[14].getLabel());
textField.setText( temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[10])) {
if(!isChoiseOptType){
value= textField.getText();
if(value.lastIndexOf(".")==value.length()-1){
value=value.substring(0,value.length()-1);
}
optValue=value;
temp=new StringBuffer("");
}
optType=b[10].getLabel();
isChoiseOptType=true;