import javaawt;
import javaawtevent;
public class lvhaiya{
int tmp,sum,sum1,sum2=1,sum3=1;
String a,b,c,d;String s="";
Frame f=new Frame("计算器");
private String[]name={
"0","1","2","3","4","5","6","7","8","9","+","-","","/","=","空格"
};
public Button[] button=new Button[namelength];
TextField t=new TextField("",30);
Panel p=new Panel();
Panel p1=new Panel();
Color color=new Color(100,170,90);
public lvhaiya(){
p1setLayout(new GridLayout(5,5));
for(int i=0;i<namelength;i++){
button[i]=new Button(name[i]);
p1add(button[i]);
}
psetLayout(new FlowLayout(FlowLayoutLEFT));
psetBackground(color);
padd(t);
fadd(p,BorderLayoutNORTH);
fadd(p1,BorderLayoutSOUTH);
fpack();
fsetVisible(true);
faddWindowListener( new WindowClose());
tsetText("0");
for(int i=0;i<namelength;i++){
button[i]addActionListener(new ButtonEventl());
}
}
public static void main(String[]args){
new lvhaiya();
}
class WindowClose extends WindowAdapter{
public void windouClosing(WindowEvent e){
Systemexit(0);
}
}
class ButtonEventl implements ActionListener{
public void actionPerformed(ActionEvent e)throws ArithmeticException{
String command=egetActionCommand();
if(commandequals("+")){
sum=IntegerparseInt(tgetText())+sum;
tsetText(StringvalueOf(sum));
a=tgetText();
s="";
}
else if(commandequals("-")){
sum1=IntegerparseInt(tgetText());
b=tgetText();
s="";
}
else if(commandequals("")){
sum2=IntegerparseInt(tgetText())sum2;
tsetText(StringvalueOf(sum2));
c=tgetText();
s="";
}
else if(commandequals("/")){
sum3=IntegerparseInt(tgetText());
d=tgetText();
s="";
}
else if(commandequals("=")){
if(a!=null){
tsetText(StringvalueOf(sum+tmp));
}
if(b!=null){
tsetText(StringvalueOf(sum1-tmp));
}
if(c!=null){
tsetText(StringvalueOf(sum2tmp));
}
try{
if(d!=null){
tsetText(StringvalueOf(sum3/tmp));
}
}
catch(ArithmeticException a){
tsetText("除数不能为零");
}
}
else if(commandequals("空格")){
sum=0;
a=null;
b=null;
c=null;
d=null;
sum1=0;
sum2=1;
sum3=1;
tmp=0;
tsetText("0");
s="";
}
else{
s=s+command;
tsetText(s);
tmp=IntegerparseInt(s);
}
}
}
}
//按钮可以自己美化一下 希望可以帮到你
欢迎分享,转载请注明来源:品搜搜测评网