import java.awt.*;
import java.awt.event.*;
public class JiSuanQi
{ static int shu1=0,shu2=0,yunsuanfu=0;
static String show="";
static Button btn[]=new Button[16];
static TextField tf=new TextField("0",20);
static String s[]={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};
public static void main(String a[])
{
Frame frm=new Frame("计算器");
frm.setLayout(null);
Panel pnl=new Panel();
GridLayout grid=new GridLayout(4,4);
pnl.setLayout(grid);
for(int j=0;j<16;j++)
{
btn[j]=new Button(s[j]);
btn[j].addActionListener(new ActionHandler());
pnl.add(btn[j]);
}
tf.setBackground(Color.orange);
tf.setBounds(20,30,160,20);
pnl.setBounds(20,60,160,80);
frm.add(tf);
frm.add(pnl);
frm.setSize(200,150);
frm.setVisible(true);
}
static class ActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Button b=(Button)e.getSource();
for(int i=0;i<16;i++)
{
if(b!=btn[3]&&b!=btn[7]&&b!=btn[11]&&b!=btn[14]&&b!=btn[15]&&b==btn[i])
{
show=show+s[i];
tf.setText(show);
}
else if(b==btn[3])
{
shu1=Integer.parseInt(show);
show=show+s[3];
tf.setText(show);
yunsuanfu=4;
}
else if(b==btn[7])
{
shu1=Integer.parseInt(show);
show=show+s[7];
tf.setText(show);
yunsuanfu=3;
}
else if(b==btn[11])
{
shu1=Integer.parseInt(show);
show=show+s[11];
tf.setText(show);
yunsuanfu=2;
}
else if(b==btn[15])
{
shu1=Integer.parseInt(show);
show=show+s[15];
tf.setText(show);
yunsuanfu=1;
}
else if(b==btn[14])
{
char m[]=show.toCharArray();
int chang=m.length;
int result=0,ysf=0;
switch(yunsuanfu)
{
case 1:ysf=show.indexOf("+");shu2=Integer.parseInt(new String(m,ysf+1,chang-1));result=shu1+shu2;break;
case 2:ysf=show.indexOf("-");shu2=Integer.parseInt(new String(m,ysf+1,chang-1));result=shu1-shu2;break;
case 3:ysf=show.indexOf("*");shu2=Integer.parseInt(new String(m,ysf+1,chang-1));result=shu1*shu2;break;
case 4:ysf=show.indexOf("/");shu2=Integer.parseInt(new String(m,ysf+1,chang-1));result=shu1/shu2;break;
default :break;
}
//show=valueOf(result);
//tf.setText(show);
System.out.println(result);
}
}
}
}
}
import java.awt.event.*;
public class JiSuanQi
{ static int shu1=0,shu2=0,yunsuanfu=0;
static String show="";
static Button btn[]=new Button[16];
static TextField tf=new TextField("0",20);
static String s[]={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};
public static void main(String a[])
{
Frame frm=new Frame("计算器");
frm.setLayout(null);
Panel pnl=new Panel();
GridLayout grid=new GridLayout(4,4);
pnl.setLayout(grid);
for(int j=0;j<16;j++)
{
btn[j]=new Button(s[j]);
btn[j].addActionListener(new ActionHandler());
pnl.add(btn[j]);
}
tf.setBackground(Color.orange);
tf.setBounds(20,30,160,20);
pnl.setBounds(20,60,160,80);
frm.add(tf);
frm.add(pnl);
frm.setSize(200,150);
frm.setVisible(true);
}
static class ActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Button b=(Button)e.getSource();
for(int i=0;i<16;i++)
{
if(b!=btn[3]&&b!=btn[7]&&b!=btn[11]&&b!=btn[14]&&b!=btn[15]&&b==btn[i])
{
show=show+s[i];
tf.setText(show);
}
else if(b==btn[3])
{
shu1=Integer.parseInt(show);
show=show+s[3];
tf.setText(show);
yunsuanfu=4;
}
else if(b==btn[7])
{
shu1=Integer.parseInt(show);
show=show+s[7];
tf.setText(show);
yunsuanfu=3;
}
else if(b==btn[11])
{
shu1=Integer.parseInt(show);
show=show+s[11];
tf.setText(show);
yunsuanfu=2;
}
else if(b==btn[15])
{
shu1=Integer.parseInt(show);
show=show+s[15];
tf.setText(show);
yunsuanfu=1;
}
else if(b==btn[14])
{
char m[]=show.toCharArray();
int chang=m.length;
int result=0,ysf=0;
switch(yunsuanfu)
{
case 1:ysf=show.indexOf("+");shu2=Integer.parseInt(new String(m,ysf+1,chang-1));result=shu1+shu2;break;
case 2:ysf=show.indexOf("-");shu2=Integer.parseInt(new String(m,ysf+1,chang-1));result=shu1-shu2;break;
case 3:ysf=show.indexOf("*");shu2=Integer.parseInt(new String(m,ysf+1,chang-1));result=shu1*shu2;break;
case 4:ysf=show.indexOf("/");shu2=Integer.parseInt(new String(m,ysf+1,chang-1));result=shu1/shu2;break;
default :break;
}
//show=valueOf(result);
//tf.setText(show);
System.out.println(result);
}
}
}
}
}
你这么变就可以了 b.equals(btn[3])不过你的取数字的逻辑没看懂,
最好的逻辑是:点击任何按键,文本框监听事件,这个时候不需要知道点击的是那个按键,当点击“=”的时候,取出文本框中的字符串进行分析,
字符串最好这么分析,因为你要先计算乘除:两个栈,一个里存放操作数,一个里存放运算符,然后对栈进行操作,