源代码:
import java.awt.*; 
import java.awt.event.*; 
import java.lang.*; 
import javax.swing.*; 
public class jsq2 extends Frame 

JTextField tf1;
Panel p0,p1,p2,p3;
GridLayout gl1,gl3;  
Button b0,b1,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26; 
TextField tf2;      
int z;       
StringBuffer str; 
static double m; 
double x,y; 
public jsq2() 

setTitle(" 计算器");
tf1=new JTextField(27); 
tf1.setHorizontalAlignment(JTextField.RIGHT); 
tf1.setEnabled(false);
tf1.setBackground(Color.green); 
tf1.setText("0"); 
tf2=new TextField(10); 
tf2.setEditable(false); 
gl1=new GridLayout(1,4,10,0);  
gl3=new GridLayout(4,5,10,15); 
b0=new Button("Backspace"); 
b0.setForeground(Color.black); 
b0.setBackground(Color.green);
b0.addActionListener(new Bt()); 
b1=new Button("CE"); 
b1.setForeground(Color.blue); 
b1.setBackground(Color.green);
b1.addActionListener(new Bt()); 
b7=new Button("7"); 
b7.setForeground(Color.black); 
b7.setBackground(Color.green);
b7.addActionListener(new Bt()); 
b8=new Button("8"); 
b8.setForeground(Color.black); 
b8.setBackground(Color.green);
b8.addActionListener(new Bt()); 
b9=new Button("9"); 
b9.setForeground(Color.black);
b9.setBackground(Color.green); 
b9.addActionListener(new Bt()); 
b10=new Button("/"); 
b10.setForeground(Color.black); 
b10.setBackground(Color.green);
b10.addActionListener(new Bt()); 
b11=new Button("sqrt"); 
b11.setForeground(Color.black);
b11.setBackground(Color.green); 
b11.addActionListener(new Bt()); 
b12=new Button("4"); 
b12.setForeground(Color.black);
b12.setBackground(Color.green); 
b12.addActionListener(new Bt()); 
b13=new Button("5"); 
b13.setForeground(Color.black);
b13.setBackground(Color.green); 
b13.addActionListener(new Bt()); 
b14=new Button("6"); 
b14.setForeground(Color.black);
b14.setBackground(Color.green); 
b14.addActionListener(new Bt()); 
b15=new Button("*"); 
b15.setForeground(Color.blue); 
b15.setBackground(Color.green);
b15.addActionListener(new Bt()); 
b16=new Button("%"); 
b16.setForeground(Color.black);
b16.setBackground(Color.green); 
b16.addActionListener(new Bt()); 
b17=new Button("1"); 
b17.setForeground(Color.black); 
b17.setBackground(Color.green);
b17.addActionListener(new Bt()); 
b18=new Button("2"); 
b18.setForeground(Color.black);
b18.setBackground(Color.green); 
b18.addActionListener(new Bt()); 
b19=new Button("3"); 
b19.setForeground(Color.black); 
b19.setBackground(Color.green);
b19.addActionListener(new Bt()); 
b20=new Button("-"); 
b20.setForeground(Color.blue); 
b20.setBackground(Color.green);
b20.addActionListener(new Bt()); 
b21=new Button("exit"); 
b21.setForeground(Color.black); 
b21.setBackground(Color.green);
b21.addActionListener(new Bt()); 
b22=new Button("0"); 
b22.setForeground(Color.black); 
b22.setBackground(Color.green);
b22.addActionListener(new Bt()); 
b23=new Button("+/-"); 
b23.setForeground(Color.black); 
b23.setBackground(Color.green);
b23.addActionListener(new Bt()); 
b24=new Button("."); 
b24.setForeground(Color.black);
b24.setBackground(Color.green); 
b24.addActionListener(new Bt()); 
b25=new Button("+"); 
b25.setForeground(Color.blue); 
b25.setBackground(Color.green);
b25.addActionListener(new Bt()); 
b26=new Button("="); 
b26.setForeground(Color.blue);
b26.setBackground(Color.green); 
b26.addActionListener(new Bt()); 
p0=new Panel(); 
p1=new Panel();
p2=new Panel(); 
p3=new Panel(); 
str=new StringBuffer(); 
p0.add(tf1); 
p0.setBounds(10,25,300,40); 
p1.setLayout(gl1);  
p1.add(b0); 
p1.add(b1);  
p1.setBounds(10,65,300,25); 
p3.setLayout(gl3); 
p3.add(b7); 
p3.add(b8); 
p3.add(b9); 
p3.add(b10); 
p3.add(b11); 
p3.add(b12); 
p3.add(b13); 
p3.add(b14); 
p3.add(b15); 
p3.add(b16); 
p3.add(b17); 
p3.add(b18); 
p3.add(b19); 
p3.add(b20); 
p3.add(b21); 
p3.add(b22); 
p3.add(b23); 
p3.add(b24); 
p3.add(b25); 
p3.add(b26); 
p3.setBounds(10,110,300,150); 
setLayout(null); 
add(p0); 
add(p1); 
add(p2); 
add(p3); 
setResizable(false); 
addWindowListener(new WindowAdapter(){ 
public void windowClosing(WindowEvent e1) 

System.exit(0); 

}); 
setBackground(Color.red); 
setBounds(100,100,320,280); 
setVisible(true); } 
class Bt implements ActionListener 

public void actionPerformed(ActionEvent e2) 

try{ if(e2.getSource()==b1) 

tf1.setText("0"); 
str.setLength(0); 

else if(e2.getSource()==b23) 

x=Double.parseDouble(tf1.getText().trim()); 
tf1.setText(""+(-x)); 

else if(e2.getSource()==b25) 

x=Double.parseDouble(tf1.getText().trim()); 
str.setLength(0); 
y=0d; 
z=0; 

else if(e2.getSource()==b20) 

x=Double.parseDouble(tf1.getText().trim()); 
str.setLength(0); 
y=0d; z=1; 

else if(e2.getSource()==b15) 

x=Double.parseDouble(tf1.getText().trim()); 
str.setLength(0); 
y=0d; 
        z=2; 

else if(e2.getSource()==b10) 

x=Double.parseDouble(tf1.getText().trim()); 
str.setLength(0); 
y=0d; 
z=3; 

else if(e2.getSource()==b26) 

str.setLength(0); 
switch(z) 

case 0 : tf1.setText(""+(x+y));break; 
case 1 : tf1.setText(""+(x-y));break; 
case 2 : tf1.setText(""+(x*y));break; 
case 3 : tf1.setText(""+(x/y));break; 


else if(e2.getSource()==b24) 

if(tf1.getText().trim().indexOf(".")!=-1) { } 
else 

if(tf1.getText().trim().equals("0"))//如果初时显示为0 

str.setLength(0); 
tf1.setText((str.append("0"+e2.getActionCommand())).toString()); 

else if(tf1.getText().trim().equals(""))//如果初时显示为空则不做任何操作 { 

else 

tf1.setText(str.append(e2.getActionCommand()).toString()); 


y=0d;

else if(e2.getSource()==b11) 

x=Double.parseDouble(tf1.getText().trim()); 
tf1.setText("数字格式异常"); 
if(x<0) 
tf1.setText("负数没有平方根"); 
else 
tf1.setText(""+Math.sqrt(x)); 
str.setLength(0); 
y=0d; 

else if(e2.getSource()==b16) 

x=Double.parseDouble(tf1.getText().trim()); 
tf1.setText(""+(0.01*x)); 
str.setLength(0); 
y=0d; 

else if(e2.getSource()==b21) 

System.exit(0);

else 

if(e2.getSource()==b22) 

if(tf1.getText().trim().equals("0")) 


else 

tf1.setText(str.append(e2.getActionCommand()).toString()); 
y=Double.parseDouble(tf1.getText().trim()); 


else if(e2.getSource()==b0) 

if(!tf1.getText().trim().equals("0")) 

if(str.length()!=1) 

tf1.setText(str.delete(str.length()-1,str.length()).toString());} 
else 

tf1.setText("0"); 
str.setLength(0); 


y=Double.parseDouble(tf1.getText().trim()); 

else 

tf1.setText(str.append(e2.getActionCommand()).toString()); 
y=Double.parseDouble(tf1.getText().trim()); 



catch(NumberFormatException e){ 
tf1.setText("数字格式异常"); 

catch(StringIndexOutOfBoundsException e){ 
tf1.setText("字符串索引越界"); 



public static void main(String args[]) 

new jsq2();

}

解决方案 »

  1.   

    给你个程序作参考吧!由于1段太长,分2段给你,你把它接起来就可以运行了.import java.awt.*;
    import java.awt.event.*;
    public class Calculator implements ActionListener
    {
    Frame f;
    MenuBar mb1;
    Menu mf1,me1,mh1,md1;
    Panel p1,p2,p3;
    TextField tf1,tf2;
    Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27;
    int Ms=0;//用于标识M+,Msdouble m=0;//用于实现连续运算
    double k=0;//用于实现负号
    double cur=0;//用于保存记忆的值
    double k1;//用于实现Backspace
    public Calculator()
    {
      f=new Frame("计算器");
      f.setSize(260,200);
      f.setLocation(320,320);
      f.setLayout(new BorderLayout());
      f.setBackground(Color.lightGray);
      tf1=new TextField(30);
      tf1.setEditable(false);
      tf1.setBackground(Color.WHITE);
      
      
      f.add(tf1,BorderLayout.CENTER);
      
      p1=new Panel(new BorderLayout());
        
      
      f.add(p1,BorderLayout.SOUTH);
      tf2=new TextField(2);
      tf2.setEditable(false);  b1=new Button("BackSpace");
      b1.setForeground(Color.RED);
      
      
      b2=new Button(" CE ");
      b2.setForeground(Color.RED);  b3=new Button(" C ");
      b3.setForeground(Color.RED);
      b2.addActionListener(this);
      b3.addActionListener(this);
      b1.addActionListener(this);
      p2=new Panel(new FlowLayout(FlowLayout.RIGHT));
      p2.add(tf2);
      p2.add(b1);
      p2.add(b2);
      p2.add(b3);
        b24=new Button("MC");
      b24.setForeground(Color.RED);
      b24.addActionListener(this);
      
      b25=new Button("MR");
      b25.setForeground(Color.RED);
      b25.addActionListener(this);
      
      b26=new Button("MS");
      b26.setForeground(Color.RED);
      b26.addActionListener(this);
      
      b27=new Button("M+");
      b27.setForeground(Color.RED);
      b27.addActionListener(this);
      
      b19=new Button("/");
      b19.setForeground(Color.RED);
      b19.addActionListener(this);
      
      b18=new Button("*");
      b18.setForeground(Color.RED);
      b18.addActionListener(this);
      
      b17=new Button("-");
      b17.setForeground(Color.RED);
      b17.addActionListener(this);
      
      b16=new Button("+");
      b16.setForeground(Color.RED);
      b16.addActionListener(this);
      
      b14=new Button("+/-");
      b14.addActionListener(this);
      
      b15=new Button(".");
      b15.addActionListener(this);
      
      b20=new Button("sqrt");
      b20.addActionListener(this);
      
      b23=new Button("%");
      b23.addActionListener(this);
      
      b21=new Button("1/x");
      b21.addActionListener(this);
      
      b22=new Button("=");
      b22.setForeground(Color.RED);
      b22.addActionListener(this);
      
      b4=new Button("7");
      b5=new Button("8");
      b6=new Button("9");
      b7=new Button("4");
      b8=new Button("5");
      b9=new Button("6");
      b10=new Button("1");
      b11=new Button("2");
      b12=new Button("3");
      b13=new Button("0");
      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);
      p3=new Panel(new GridLayout(4,6,2,2));
      p3.add(b24); 
      p3.add(b4);
      p3.add(b5);
      p3.add(b6);
      p3.add(b21);
      p3.add(b19);
      p3.add(b20);
      p3.add(b25);
      p3.add(b7);
      p3.add(b8);
      p3.add(b9);
      p3.add(b18);
      p3.add(b23);
      p3.add(b26);
      p3.add(b10);
      p3.add(b11);
      p3.add(b12);
      p3.add(b17);
      p3.add(b21);
      p3.add(b27);
      p3.add(b13);
      p3.add(b14);
      p3.add(b15);
      p3.add(b16);
      p3.add(b22);  
        
      p1.add(p2,BorderLayout.NORTH);
      p1.add(p3,BorderLayout.CENTER);
      f.add(p1,BorderLayout.SOUTH);
      
      f.addWindowListener(new WinClose());
      f.setVisible(true);
      tf1.setText("");
      addmyMenu();
    }
        public void addmyMenu()
        {
            mb1 = new MenuBar();            //生成一个菜单栏
            f.setMenuBar(mb1);              //框架f上添加菜单栏
            mf1 = new Menu("编辑(E)");      //生成一个菜单
            me1 = new Menu("查看(V)");
            mh1 = new Menu("帮助(H)");
            mb1.add(mf1);                    //菜单栏中加入菜单
            mb1.add(me1);
            mb1.add(mh1);
            mf1.add(new MenuItem("复制"));  //生成菜单项并加入到菜单
            mf1.add(new MenuItem("粘贴"));
            mf1.addActionListener(this);    //为菜单注册事件监听程序
            me1.add(new MenuItem("标准型"));
            me1.add(new MenuItem("科学型"));
            me1.addSeparator();              //加分隔线
            me1.add(new MenuItem("数字分组"));
            me1.addActionListener(this);
            mh1.add(new MenuItem("帮助主题"));  
            mh1.add(new MenuItem("关于计算器"));
            mh1.addActionListener(this);
        }
    int flag=0;//运算符有效标识
    int num=0;//用于实现连续运算
    double dot=0;//用于标识小数点
    double first=0,second=0,result=0;//用于保存第一个数,第二个数和计算结果
    double push;//用于标识是否有数字键按下 
      

  2.   

    还有下面的1段.你自己接吧.public void actionPerformed(ActionEvent e)
    {  if(e.getSource()==b3)
      {tf1.setText("0."); 
      num=0;
      dot=0;
      push=0;
      flag=0;
      first=0;} 
      else
      
      if(e.getSource()==b4)
    {
      if(push==0.)
      tf1.setText(e.getActionCommand());
      else
      tf1.setText(tf1.getText()+e.getActionCommand());
      push=1;}
      if(e.getSource()==b5)
      {
      if(push==0.)
      tf1.setText(e.getActionCommand());
      else
      tf1.setText(tf1.getText()+e.getActionCommand());
      push=1;}
      if(e.getSource()==b6)
      {
      if(push==0.)
      tf1.setText(e.getActionCommand());
      else
      tf1.setText(tf1.getText()+e.getActionCommand());
      push=1;}
      if(e.getSource()==b7)
      {
      if(push==0.)
      tf1.setText(e.getActionCommand());
      else
      tf1.setText(tf1.getText()+e.getActionCommand());
      push=1;}
      if(e.getSource()==b8)
      {
      if(push==0.)
      tf1.setText(e.getActionCommand());
      else
      tf1.setText(tf1.getText()+e.getActionCommand());
      push=1;}
      if(e.getSource()==b9)
      {
      if(push==0.)
      tf1.setText(e.getActionCommand());
      else
      tf1.setText(tf1.getText()+e.getActionCommand());
      push=1;}
      if(e.getSource()==b10)
      {
      if(push==0.)
      tf1.setText(e.getActionCommand());
      else
      tf1.setText(tf1.getText()+e.getActionCommand());
      push=1;}
      if(e.getSource()==b11)
      {
      if(push==0.)
      tf1.setText(e.getActionCommand());
      else
      tf1.setText(tf1.getText()+e.getActionCommand());
      push=1;}
      if(e.getSource()==b12)
      {
      if(push==0.)
      tf1.setText(e.getActionCommand());
      else
      tf1.setText(tf1.getText()+e.getActionCommand());
      push=1;}
      if(e.getSource()==b13)
      {
      if(push==0.)
      tf1.setText(e.getActionCommand());
      else
      tf1.setText(tf1.getText()+e.getActionCommand());}
      
      
      if(e.getSource()==b26||e.getSource()==b27)
      {
      
      tf2.setText("M");
      cur=Double.parseDouble(tf1.getText());
      Ms=1;
      }
      if(e.getSource()==b25)
      {
      if(Ms==1)
      tf1.setText(Double.toString(cur));
      else
      tf1.setText("0.");
      }
      if(e.getSource()==b24)
      {
      tf2.setText(" ");
          Ms=0;
      }
      if(e.getSource()==b14)
        {
          k=Double.parseDouble(tf1.getText());
          k=-k;
          tf1.setText(Double.toString(k));
          }
        if(e.getSource()==b15)
      {
      push=1;
      if(dot==0)
      tf1.setText(tf1.getText()+e.getActionCommand());
      else
      tf1.setText(tf1.getText());
      dot=1;}
      if(e.getSource()==b16)
      {
        flag=1;
        first=Double.parseDouble(tf1.getText());
        dot=0;
        push=0;
      }
      if(e.getSource()==b17)
      {
        flag=2;
        first=Double.parseDouble(tf1.getText());
        dot=0;
        push=0;
      }
      if(e.getSource()==b18)
      {
        flag=3;
        first=Double.parseDouble(tf1.getText());
        dot=0;
        push=0;
      }
      if(e.getSource()==b19)
      {
        flag=4;
        first=Double.parseDouble(tf1.getText());
        dot=0;
        push=0;
      }
      if(e.getSource()==b21)
      {
        flag=5;
        first=Double.parseDouble(tf1.getText());
        dot=0;
        push=0;
      }
      if(e.getSource()==b23)
      {
        flag=6;
        first=Double.parseDouble(tf1.getText());
        dot=0;
        push=0;
      }
      if(e.getSource()==b20)
      {
        flag=7;
        first=Double.parseDouble(tf1.getText());
        dot=0;
        push=0;
      }
      if(e.getSource()==b22)
      {
        
        second=Double.parseDouble(tf1.getText());
        if(num==0)
        m=second;
        num=1;
        switch(flag)
        {
        case 0: break;
        case 1: result=first+second;
          first=m;
          tf1.setText(String.valueOf(result));
          break;
        case 2:second=m;
          result=first-second;
          first=result;
              tf1.setText(String.valueOf(result));
          break;
        case 3:result=first*second;
          first=m;
          tf1.setText(String.valueOf(result));
          break;
        case 4:if(second==0)
          tf1.setText("除数不能为0");
          else
          {second=m;
          result=first/second;
          first=result;
          tf1.setText(String.valueOf(result));}
          break;
        case 5:if(first==0)
          tf1.setText("0没有倒数");
          else
          {result=1/first;
          first=result;
          tf1.setText(String.valueOf(result));}
          break;
        case 6:result=first/100;
          first=result;
          tf1.setText(String.valueOf(result));
          break;
        case 7:if(first<0)
          tf1.setText("负数不能开方"); 
          else{result=Math.sqrt(first);
                        first=result;
                        tf1.setText(String.valueOf(result));}
                      
                      break;  
        }
        dot=0;
        push=0;
      }
      if(e.getSource()==b2)
      {
        tf1.setText("0.");
      }
      if(e.getSource()==b1)
      {
        k1=Double.parseDouble(tf1.getText());
        if(k1==0)
        tf1.setText(Double.toString(k1));
        String s=tf1.getText();
        int i;
        i=s.length();
        s=s.substring(0,i-1);
        i=i-1;
        if(i==0)
        tf1.setText("0.");
        else
        tf1.setText(s);
        push=0;
        dot=0;
      }
      }
    public static void main(String arg[])
    {
      new Calculator();
    }
    }
    class WinClose extends WindowAdapter
    {
      public void windowClosing(WindowEvent e)
      {
      System.exit(0);
      }
    }
      

  3.   

    先参考windows的计算器,里面的逻辑在使用时就很清楚的体现出来了,在输入了3+4之后,随便按什么运算符,上一步的结果就已经出来了,是7,然后再执行后面的运算操作
      

  4.   

    回5楼,sorry,我错了。Windows的标准型计算机是不处理运算符优先级的,呵呵;科学型有处理
      

  5.   

    这个是多么容易的事情啊,网上找找表达式求值的资料吧,不过都是C语言写的,可是C语言比Java简单多了。主要还是利用栈。建立一个操作数栈,和一个符号栈。
      

  6.   

    楼上说的很对,连续运算的问题,就是需要借助栈来实现,不过,Java里面提供了栈类Stack,拿来用就可以了,不需要自己设计,这是Java比C好的地方。
      

  7.   

    这是我做的一个计算器,错误地方请指教import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Calculator extends JFrame{
        private ShowText show;
        private final int W=250,H=300;
        public Calculator(){
         show=new ShowText();
         setSize(W,H);
         show.setFont(new Font("宋体",Font.BOLD,30));
         Container con=getContentPane();
         con.add(show,"North");
         Container cb=new Container();
         cb.setLayout(new GridLayout(4,4));
         addButton(cb);
         con.add(cb);
         setResizable(false);
         Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
         setLocation((dim.width-W)/2,(dim.height-H)/2);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        private void addButton(Container con){
         for(int i=1;i<10;i++){
         con.add(new NButton((char)(i+48)));
         if(i==3)
         con.add(new NButton('+'));
         else if(i==6)
         con.add(new NButton('-'));
         else if(i==9)
         con.add(new NButton('*'));
         }
         con.add(new NButton('0'));
         con.add(new NButton('.'));
         con.add(new NButton('='));
         con.add(new NButton('/'));
        
        }
        class NButton extends JButton{
         private char name;
         public NButton(char ch){
         super(String.valueOf(ch));
         name=ch;
         this.setFont(new Font("宋体",Font.BOLD,30));
        enableEvents(AWTEvent.MOUSE_EVENT_MASK);
         }
         public char getNum(){
         return name;
         }
         public void processMouseEvent(MouseEvent e){
         if(e.getID()==MouseEvent.MOUSE_CLICKED){
         show.appendChar(name);
         }
         }
        }
        class ShowText extends JTextField {
         double dividend,divisor;
         char sign;
         boolean over=false,start=false,end=false,mid=false;
         public ShowText(){
         setHorizontalAlignment(JTextField.RIGHT);
         setEditable(false);
         enableEvents(AWTEvent.INPUT_METHOD_EVENT_MASK);
         }
         public double getResult(char ch){
         switch(ch){
         case '+':
         return dividend+divisor;
         case '-':
         return dividend-divisor;
         case '*':
         return dividend*divisor;
         default:
         if(divisor!=0)
         return dividend/divisor;
         else
         return 0.0;
         }
        
         }
         public void appendChar(char ch){
         if(ch<48&&ch!=46){
         if(start){
         sign=ch;
         dividend=Double.valueOf(getText());
         setText("");
         mid=true;
         }
         }else if(ch=='='){
         if(mid&&end){
         divisor=Double.valueOf(getText());
         setText(String.valueOf(getResult(sign)));
         over=true;
         end=false;
         }
         }else{
         if(ch!='.')start=true;
         if(mid&&ch!='.')end=true;
         if(over){
         setText(String.valueOf(ch));
         over=false;
         }else{
         String s=getText()+ch;
         setText(s);
         }
         System.out.println(start+"::"+end);
         }
         }
        }
        public static void main(String[] args) {
         Calculator cal=new Calculator();
         cal.setVisible(true);
         int a='.';
        // System.out.println(String.valueOf(.));
        }
    }
      

  8.   

    不知道JAVA里能不能调用JS里的函数哇? 能的话直接用 EVAL就行了。
      

  9.   

    看看我BLOG上J2ME的计算器的代码或许对你有启示