| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1134 人关注过本帖, 1 人收藏
标题:Javapet版的计算器!!
只看楼主 加入收藏
Javapet
Rank: 1
等 级:禁止访问
帖 子:80
专家分:0
注 册:2008-1-5
结帖率:50%
收藏(1)
 问题点数:0 回复次数:6 
Javapet版的计算器!!
近日做了一个简单的计算器,希望对一些初学的朋友有帮助,当然也希望前辈们能指点指点,使我的程序能得到优化!
共同学习,共同进步,共同迈向......!
package Counter;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Counter extends JFrame
{
        StringBuffer str1=null;
        StringBuffer str2=null;
        StringBuffer str3=null;
        StringBuffer str4=null;
      Button[] B=new Button[27];
      JTextField JText=null;
      TextField Text=null;
        int flag=5,v=0,z=0,m=0,q=0,w=0;
      boolean bool=false;
      
      public Counter(String str)
      {
            super(str);
            this.setLayout(null);
            Container Container_=this.getContentPane();
            str1=new StringBuffer();
                str2=new StringBuffer();
                str3=new StringBuffer();
                str4=new StringBuffer();
            this.pet(Container_);
      }
        public void pet(Container Container_)
        {                
                //创建布局管理器
                GridLayout GridLayout1=new GridLayout(1,3,5,5);
                GridLayout GridLayout2=new GridLayout(4,1,5,5);
                GridLayout GridLayout3=new GridLayout(4,5,5,5);
                
                //实例化JPanel
                JPanel JP1=new JPanel();
                JPanel JP2=new JPanel();
                JPanel JP3=new JPanel();
                JPanel JP4=new JPanel();
                JPanel JP5=new JPanel();
                
                //创建菜单栏
                MenuBar Menu_Bar=new MenuBar();
                Menu Menu_a=new Menu("编辑");
                Menu Menu_b=new Menu("帮助");
                MenuItem Menu_Item_a=new MenuItem("复制");
                MenuItem Menu_Item_b=new MenuItem("粘贴");
                MenuItem Menu_Item_c=new MenuItem("关于计算机");
                Menu_a.add(Menu_Item_a);
                Menu_a.add(Menu_Item_b);
                Menu_b.add(Menu_Item_c);
                Menu_Bar.add(Menu_a);
                Menu_Bar.add(Menu_b);
                this.setMenuBar(Menu_Bar);
                
                //创建布置数值显示屏和记忆屏
                JText=new JTextField("0.",21);
                JText.setBackground(Color.white);
                JText.setHorizontalAlignment(JTextField.RIGHT );
                JText.setEditable(false);
                Text=new TextField(1);
                Text.setBackground(Color.white);
                Text.setEnabled(false);
              JP4.add(JText);
                JP5.add(Text);
                JP4.setBounds(2,0,250,30);
                JP5.setBounds(26,30,0,0);
                
                //创建按钮并设置文本颜色
                B[0]=new Button("MC");
                B[1]=new Button("MR");
              B[2]=new Button("MS");
                B[3]=new Button("M+");
              B[4]=new Button("Backspace");
                B[5]=new Button("CE");
                B[6]=new Button("C");
              B[7]=new Button("7");
                B[8]=new Button("8");
                B[9]=new Button("9");
                B[10]=new Button("/");
                B[11]=new Button("sqrt");
                B[12]=new Button("4");
                B[13]=new Button("5");
                B[14]=new Button("6");
              B[15]=new Button("*");
                B[16]=new Button("%");
                B[17]=new Button("1");
                B[18]=new Button("2");
                B[19]=new Button("3");
                B[20]=new Button("-");
                B[21]=new Button("1/x");
                B[22]=new Button("0");
                B[23]=new Button("+/-");
                B[24]=new Button(".");
                B[25]=new Button("+");
              B[26]=new Button("=");
                for(int i=0;i<12;i++)
                {
                    B[i].setForeground(Color.red);
                }
              for(int i=12;i<26;i++)
                {
                    B[i].setForeground(Color.blue);
                }
        
            //布置JP1
                JP1.setLayout(GridLayout1);
                JP1.add(B[4]);
                JP1.add(B[5]);
                JP1.add(B[6]);
                JP1.setBounds(50,33,197,27);
                
                //布置JP2
                JP2.setLayout(GridLayout2);
                JP2.add(B[0]);
                JP2.add(B[1]);
                JP2.add(B[2]);
                JP2.add(B[3]);
                JP2.setBounds(9,65,35,120);            
                
                //布置JP3
                JP3.setLayout(GridLayout3);
                for(int i=7;i<27;i++)
                {
                        JP3.add(B[i]);
                }
                JP3.setBounds(50,65,195,120);
                
                //添加窗口监听器
                this.addWindowListener(new WindowAdapter()
                                                             {
                                                                         public void windowClosing(WindowEvent e)
                                                                         {
                                                                                 System.exit(0);
                                                                         }
                                                             }
                                                            );
                //添加按钮监听器
                for(int i=0;i<27;i++)
                {
                        B[i].addActionListener(new monitor(this));                            
                }    
              
              //添加菜单监听器
              Menu_Item_a.addActionListener(new ActionListener()
                                                                          {
                                                                                  public void actionPerformed(ActionEvent e)
                                                                                    {
                                                                                            str4.append(JText.getText());
                                                                                    }
                                                                          });
              Menu_Item_b.addActionListener(new ActionListener()
                                                                          {
                                                                                  public void actionPerformed(ActionEvent e)
                                                                                    {
                                                                                            JText.setText(str4.toString());
                                                                                            str4.setLength(0);
                                                                                    }
                                                                          });                                                        
              Menu_Item_c.addActionListener(new ActionListener()
                                                                          {
                                                                                  public void actionPerformed(ActionEvent e)
                                                                                    {
                                                                                            JDialog JD=new JDialog();
                                                                                JD.setSize(150,200);
                                                                                JD.setLocation(350,300);
                                                                                            JD.getContentPane().add(new JLabel("共同学习交流,彼此进步!"));
                                                                                            JD.setVisible(true);
                                                                                    }
                                                                          });
              //容器整合
              Container_.add(JP1);
                Container_.add(JP2);
                Container_.add(JP3);
        Container_.add(JP4);
        Container_.add(JP5);   
        }
        public void paintComponent(Graphics g)
      {
          Graphics2D g1=(Graphics2D)g;
          g1.setColor(Color.white);
    }        
    public static void main(String[] args)
        {
                Counter C=new Counter("计算器(Javapet制作)");
                C.setSize(260,246);
                C.setLocation(200,200);
                C.setResizable(false);
                C.setVisible(true);
        }
}
class monitor implements ActionListener
{
      Counter C;
      Double t=0.0;
      Double x=0.0,y=0.0;
      
      public monitor(Counter C)
      {
                this.C=C;
      }
        public void actionPerformed(ActionEvent e)
        {
              try
              {
                      if(e.getSource()==C.B[0])
                      {
                            C.str1.setLength(0);
                              C.str3.setLength(0);
                              C.Text.setText("");
                              C.q=1;
                      }
                      if(e.getSource()==C.B[1])
                      {
                            C.str1.setLength(0);
                            if(C.q!=0)
                            {
                                    C.JText.setText("0.");
                            }
                              else
                              {
                                      C.JText.setText(C.str3.toString());
                        }
                      }
                      else if(e.getSource()==C.B[2])
                      {
                            if(C.w!=0)
                            {
                                    C.str3.setLength(0);
                            }
                              C.str3.append(C.JText.getText());
                              C.Text.setText(" "+"M");
                              C.str1.setLength(0);
                            C.w=1;
                      }
                      else if(e.getSource()==C.B[3])
                      {
                            if(C.w!=0)
                            {
                                    C.str3.setLength(0);
                            }
                              C.str3.append(C.JText.getText());
                              C.Text.setText(" "+"M");
                              C.str1.setLength(0);
                            C.w=1;
                      }
                      else if(e.getSource()==C.B[4])
                      {
                            if(!C.bool)
                            {
                                  C.str1.delete(C.str1.length()-1,C.str1.length());
                                  C.JText.setText(C.str1.toString());
                          }
                          else
                          {
                                  C.str2.delete(C.str2.length()-1,C.str2.length());
                                  C.JText.setText(C.str2.toString());
                          }
                    }
                    else if(e.getSource()==C.B[5])
                    {
                            C.str1.setLength(0);
                          C.str2.setLength(0);
                          C.JText.setText("0.");
                          C.bool=false;
                          C.v=0;    
                          C.z=0;
                          C.q=0;
                          C.w=0;
                    }
                    else if(e.getSource()==C.B[6])
                      {
                          C.str1.setLength(0);
                          C.str2.setLength(0);
                          C.JText.setText("0.");
                          C.bool=false;
                          C.v=0;
                          C.z=0;
                          C.q=0;
                          C.w=0;
                    }
                        else if(e.getSource()==C.B[7])
                        {
                              if(!C.bool)
                            {
                                  if(C.str1.length()<8)
                                  {                                   
                                        C.JText.setText(C.str1.toString()+"7");
                                        C.str1.append("7");
                                }
                            }
                            else
                            {
                                    if(C.str2.length()<8)
                                  {                                   
                                        C.JText.setText(C.str2.toString()+"7");
                                        C.str2.append("7");
                                }
                            }
                        }                
                        else if(e.getSource()==C.B[8])
                        {   
                                if(!C.bool)
                            {
                                  if(C.str1.length()<8)
                                  {                                   
                                        C.JText.setText(C.str1.toString()+"8");
                                        C.str1.append("8");
                                }
                            }
                            else
                            {
                                    if(C.str2.length()<8)
                                  {                                   
                                        C.JText.setText(C.str2.toString()+"8");
                                        C.str2.append("8");
                                }
                            }
                        }                
                        else if(e.getSource()==C.B[9])
                        {          
                              if(!C.bool)
                            {
                                  if(C.str1.length()<8)
                                  {                                   
                                        C.JText.setText(C.str1.toString()+"9");
                                        C.str1.append("9");
                                }
                            }
                            else
                            {
                                    if(C.str2.length()<8)
                                  {                                   
                                        C.JText.setText(C.str2.toString()+"9");
                                        C.str2.append("9");
                                }
                            }
                        }                
                        else if(e.getSource()==C.B[10])
                        {
                              if(C.str2.length()!=0&&C.str1.length()!=0)
                              {
                                  switch(C.flag)
                                  {
                            
                                          case 1:
                                                  x=Double.parseDouble(C.str1.toString());
                                                  y=Double.parseDouble(C.str2.toString());
                                                  t=x+y;
                                                  C.str2.setLength(0);
                                                  C.JText.setText(String.valueOf(t));
                                                  C.str1.setLength(0);
                                                  C.str1.append(String.valueOf(t));
                                                  C.v=0;
                                                  break;
                                          case 2:
                                              x=Double.parseDouble(C.str1.toString());
                                                  y=Double.parseDouble(C.str2.toString());
                                                  t=x-y;
                                                  C.str2.setLength(0);
                                                  C.JText.setText(String.valueOf(t));
                                                  C.str1.setLength(0);
                                                  C.str1.append(String.valueOf(t));
                                                  C.v=0;
                                                  break;
                                          case 3:
                                                  x=Double.parseDouble(C.str1.toString());
                                                  y=Double.parseDouble(C.str2.toString());
                                                  t=x*y;
                                                  C.str2.setLength(0);
                                                  C.JText.setText(String.valueOf(t));
                                                  C.str1.setLength(0);
                                                  C.str1.append(String.valueOf(t));
                                                  C.v=0;
                                                  break;
                                          case 4:
                                                  x=Double.parseDouble(C.str1.toString());
                                                  y=Double.parseDouble(C.str2.toString());
                                                  t=x/y;
                                                  C.str2.setLength(0);
                                                  C.JText.setText(String.valueOf(t));
                                                  C.str1.setLength(0);
                                                  C.str1.append(String.valueOf(t));
                                                  C.v=0;
                                                  break;
                                  }
                              }
                        C.flag=4;
                        C.z=0;            
                        C.m=0;
                        if(C.str1.length()!=0)
                        {            
                                C.bool=true;
                        }                       
                        }                
                        else if(e.getSource()==C.B[11])
                        {
                              if(!C.bool)
                              {
                                      String t=String.valueOf(Math.sqrt(Double.parseDouble(C.str1.toString())));
                                      C.str1.setLength(0);
                                      C.str1.append(t);
                                      C.JText.setText(C.str1.toString());
                              }
                              else
                              {
                                      String t=String.valueOf(Math.sqrt(Double.parseDouble(C.str2.toString())));
                                      C.str2.setLength(0);
                                      C.str2.append(t);
                                      C.JText.setText(C.str2.toString());
                              }
                        }                
                        else if(e.getSource()==C.B[12])
                        {
                              if(!C.bool)
                            {
                                  if(C.str1.length()<8)
                                  {                                   
                                        C.JText.setText(C.str1.toString()+"4");
                                        C.str1.append("4");
                                }
                            }
                            else
                            {
                                    if(C.str2.length()<8)
                                  {                                   
                                        C.JText.setText(C.str2.toString()+"4");
                                        C.str2.append("4");
                                }
                            }
                        }                
                        else if(e.getSource()==C.B[13])
                        {
                             if(!C.bool)
                            {
                                  if(C.str1.length()<8)
                                  {                                   
                                        C.JText.setText(C.str1.toString()+"5");
                                        C.str1.append("5");
                                }
                            }
                            else
                            {
                                    if(C.str2.length()<8)
                                  {                                   
                                        C.JText.setText(C.str2.toString()+"5");
                                        C.str2.append("5");
                                }
                            }
                        }                
                        else if(e.getSource()==C.B[14])
                        {
                              if(!C.bool)
                            {
                                  if(C.str1.length()<8)
                                  {                                   
                                        C.JText.setText(C.str1.toString()+"6");
                                        C.str1.append("6");
                                }
                            }
                            else
                            {
                                    if(C.str2.length()<8)
                                  {                                   
                                        C.JText.setText(C.str2.toString()+"6");
                                        C.str2.append("6");
                                }
                            }
                        }        
                        else if(e.getSource()==C.B[15])
                        {
                                if(C.str2.length()!=0&&C.str1.length()!=0)
                              {
                                  switch(C.flag)
                                  {
                            
                                          case 1:
                                                  x=Double.parseDouble(C.str1.toString());
                                                  y=Double.parseDouble(C.str2.toString());
                                                  t=x+y;
                                                  C.str2.setLength(0);
                                                  C.JText.setText(String.valueOf(t));
                                                  C.str1.setLength(0);
                                                  C.str1.append(String.valueOf(t));
                                                  C.v=0;
                                                  break;
                                          case 2:
                                              x=Double.parseDouble(C.str1.toString());
                                                  y=Double.parseDouble(C.str2.toString());
                                                  t=x-y;
                                                  C.str2.setLength(0);
                                                  C.JText.setText(String.valueOf(t));
                                                  C.str1.setLength(0);
                                                  C.str1.append(String.valueOf(t));
                                                  C.v=0;
                                                  break;
                                          case 3:
                                                  x=Double.parseDouble(C.str1.toString());
                                                  y=Double.parseDouble(C.str2.toString());
                                                  t=x*y;
                                                  System.out.println(t);
                                                  C.str2.setLength(0);
                                                  C.JText.setText(String.valueOf(t));
                                                  C.str1.setLength(0);
                                                  C.str1.append(String.valueOf(t));
                                                  C.v=0;
                                                  break;
                                          case 4:
                                                  x=Double.parseDouble(C.str1.toString());
                                                  y=Double.parseDouble(C.str2.toString());
                                                  t=x/y;
                                                  C.str2.setLength(0);
                                                  C.JText.setText(String.valueOf(t));
                                                  C.str1.setLength(0);
                                                  C.str1.append(String.valueOf(t));
                                                  C.v=0;
                                                  break;
                                  }
                              }
                        C.flag=3;         
                        C.z=0;     
                        C.m=0;
                        if(C.str1.length()!=0)
                        {            
                                C.bool=true;
                        }                                
                        }                        
                        else if(e.getSource()==C.B[16])
                        {
                                if(!C.bool)
                              {
                                      String t=String.valueOf(0.01*(Double.parseDouble(C.str1.toString())));
                                      C.str1.setLength(0);
                                      C.str1.append(t);
                                      C.JText.setText(C.str1.toString());
                              }
                              else
                              {
                                      String t=String.valueOf(0.01*(Double.parseDouble(C.str2.toString())));
                                      C.str2.setLength(0);
                                      C.str2.append(t);
                                      C.JText.setText(C.str2.toString());
                              }
                        }                
                        else if(e.getSource()==C.B[17])
                        {
                             if(!C.bool)
                            {
                                  if(C.str1.length()<8)
                                  {                                   
                                        C.JText.setText(C.str1.toString()+"1");
                                        C.str1.append("1");
                                }
                            }
                            else
                            {
                                    if(C.str2.length()<8)
                                  {                                   
                                        C.JText.setText(C.str2.toString()+"1");
                                        C.str2.append("1");
                                }
                            }
                        }                
                        else if(e.getSource()==C.B[18])
                        {
                             if(!C.bool)
                            {
                                  if(C.str1.length()<8)
                                  {                                   
                                        C.JText.setText(C.str1.toString()+"2");
                                        C.str1.append("2");
                                }
                            }
                            else
                            {
                                    if(C.str2.length()<8)
                                  {                                   
                                        C.JText.setText(C.str2.toString()+"2");
                                        C.str2.append("2");
                                }
                            }
                        }                
                        else if(e.getSource()==C.B[19])
                        {
                              if(!C.bool)
                            {
                                  if(C.str1.length()<8)
                                  {                                   
                                        C.JText.setText(C.str1.toString()+"3");
                                        C.str1.append("3");
                                }
                            }
                            else
                            {
                                    if(C.str2.length()<8)
                                  {                                   
                                        C.JText.setText(C.str2.toString()+"3");
                                        C.str2.append("3");
                                }
                            }
                        }                
                        else if(e.getSource()==C.B[20])
                        {
                                if(C.str2.length()!=0&&C.str1.length()!=0)
                              {
                                  switch(C.flag)
                                  {
                            
                                          case 1:
                                                  x=Double.parseDouble(C.str1.toString());
                                                  y=Double.parseDouble(C.str2.toString());
                                                  t=x+y;
                                                  C.str2.setLength(0);
                                                  C.JText.setText(String.valueOf(t));
                                                  C.str1.setLength(0);
                                                  C.str1.append(String.valueOf(t));
                                                  C.v=0;
                                                  break;
                                          case 2:
                                              x=Double.parseDouble(C.str1.toString());
                                                  y=Double.parseDouble(C.str2.toString());
                                                  t=x-y;
                                                  C.str2.setLength(0);
                                                  C.JText.setText(String.valueOf(t));
                                                  C.str1.setLength(0);
                                                  C.str1.append(String.valueOf(t));
                                                  C.v=0;
                                                  break;
                                          case 3:
                                                  x=Double.parseDouble(C.str1.toString());
                                                  y=Double.parseDouble(C.str2.toString());
                                                  t=x*y;
                                                  C.str2.setLength(0);
                                                  C.JText.setText(String.valueOf(t));
                                                  C.str1.setLength(0);
                                                  C.str1.append(String.valueOf(t));
                                                  C.v=0;
                                                  break;
                                          case 4:
                                                  x=Double.parseDouble(C.str1.toString());
                                                  y=Double.parseDouble(C.str2.toString());
                                                  t=x/y;
                                                  C.str2.setLength(0);
                                                  C.JText.setText(String.valueOf(t));
                                                  C.str1.setLength(0);
                                                  C.str1.append(String.valueOf(t));
                                                  C.v=0;
                                                  break;
                                  }
                              }
                        C.flag=2;
                        C.z=0;           
                        C.m=0;   
                        if(C.str1.length()!=0)
                        {            
                                C.bool=true;
                        }                       
                        }                
                        else if(e.getSource()==C.B[21])
                        {
                                if(!C.bool)
                              {
                                      String t=String.valueOf(1/(Double.parseDouble(C.str1.toString())));
                                      C.str1.setLength(0);
                                      C.str1.append(t);
                                      C.JText.setText(C.str1.toString());
                              }
                              else
                              {
                                      String t=String.valueOf(1/(Double.parseDouble(C.str2.toString())));
                                      C.str2.setLength(0);
                                      C.str2.append(t);
                                      C.JText.setText(C.str2.toString());
                              }
                        }                
                        else if(e.getSource()==C.B[22])
                        {
                              if(!C.bool)
                              {
                                    if(C.str1.length()!=0)
                                    {
                                          if(C.str1.length()<8)
                                          {                                   
                                                C.JText.setText(C.str1.toString()+"0");
                                                C.str1.append("0");
                                        }
                                    }
                                    else
                                    {   
                                          if(C.v!=1)
                                          {
                                                  C.v=1;
                                                    C.JText.setText("0.");
                                                    C.str2.append("0");
                                        }
                                    }        
                            }
                            else
                            {
                                    if(C.str2.length()!=0)
                                    {
                                            if(C.str2.length()<8)
                                          {                                 
                                                C.JText.setText(C.str2.toString()+"0");
                                                C.str2.append("0");
                                        }
                                    }
                                    else
                                    {
                                          if(C.flag==4)
                                          {
                                                    C.JText.setText("不能除以0");
                                                    C.str1.setLength(0);
                                                    C.str2.setLength(0);
                                                    C.flag=5;
                                                    C.bool=false;
                                          }
                                          else if(C.v!=1)
                                          {
                                                  C.JText.setText("0.");
                                                  C.str2.append("0");
                                                  C.v=1;
                                          }                         
                                    }
                          }
                        }                
                        else if(e.getSource()==C.B[23])
                        {
                                if(!C.bool)
                                {
                                        if(C.m!=1)
                                        {
                                                C.str1.insert(0,"-");
                                                C.JText.setText(C.str1.toString());
                                                C.m=1;
                                      }
                                      else
                                      {
                                              C.str1.deleteCharAt(0);
                                              C.JText.setText(C.str1.toString());
                                              C.m=0;
                                      }
                                }
                                else
                                {
                                        if(C.m!=1)
                                        {
                                                C.str2.insert(0,"-");
                                                C.JText.setText(C.str2.toString());
                                                C.m=1;
                                      }
                                      else
                                      {
                                              C.str2.deleteCharAt(0);
                                              C.JText.setText(C.str2.toString());
                                              C.m=0;
                                      }
                                }
                        }                
                        else if(e.getSource()==C.B[24])
                        {
                                if(!C.bool)
                              {
                                      if(C.str1.length()!=0&&C.z!=1)
                                      {
                                              C.str1.append(".");
                                              C.z=1;
                                      }
                                      else
                                      {
                                            if(C.z!=1)
                                            {
                                                      C.str1.append("0.");
                                                      C.JText.setText("0.");
                                                      C.z=1;
                                            }
                                      }
                              }
                              else
                              {
                                      if(C.str2.length()!=0&&C.z!=1)
                                      {
                                              C.str2.append(".");
                                              C.z=1;
                                      }
                                      else
                                      {
                                              if(C.z!=1)
                                            {
                                                      C.str2.append("0.");
                                                      C.JText.setText("0.");
                                                      C.z=1;
                                            }
                                    }
                              }
                        }                
                        else if(e.getSource()==C.B[25])
                        {
                                if(C.str2.length()!=0&&C.str1.length()!=0)
                              {
                                  switch(C.flag)
                                  {
                                          case 1:
                                                  x=Double.parseDouble(C.str1.toString());
                                                  y=Double.parseDouble(C.str2.toString());
                                                  t=x+y;
                                                  C.str2.setLength(0);
                                                  C.JText.setText(String.valueOf(t));
                                                  C.str1.setLength(0);
                                                  C.str1.append(String.valueOf(t));
                                                  C.v=0;
                                                  break;
                                          case 2:
                                              x=Double.parseDouble(C.str1.toString());
                                                  y=Double.parseDouble(C.str2.toString());
                                                  t=x-y;
                                                  C.str2.setLength(0);
                                                  C.JText.setText(String.valueOf(t));
                                                  C.str1.setLength(0);
                                                  C.str1.append(String.valueOf(t));
                                                  C.v=0;
                                                  break;
                                          case 3:
                                                  x=Double.parseDouble(C.str1.toString());
                                                  y=Double.parseDouble(C.str2.toString());
                                                  t=x*y;
                                                  C.str2.setLength(0);
                                                  C.JText.setText(String.valueOf(t));
                                                  C.str1.setLength(0);
                                                  C.str1.append(String.valueOf(t));
                                                  C.v=0;
                                                  break;
                                          case 4:
                                                  x=Double.parseDouble(C.str1.toString());
                                                  y=Double.parseDouble(C.str2.toString());
                                                  t=x/y;
                                                  C.str2.setLength(0);
                                                  C.JText.setText(String.valueOf(t));
                                                  C.str1.setLength(0);
                                                  C.str1.append(String.valueOf(t));
                                                  C.v=0;
                                                  break;
                                  }
                              }
                        C.flag=1;  
                        C.z=0;
                        C.m=0;
                        if(C.str1.length()!=0)
                        {            
                                C.bool=true;
                        }                
                        }                
                        else if(e.getSource()==C.B[26])
                  {
                          if(C.str2.length()!=0&&C.str1.length()!=0)
                              {
                                      switch(C.flag)
                                    {
                              
                                            case 1:
                                                    x=Double.parseDouble(C.str1.toString());
                                                    y=Double.parseDouble(C.str2.toString());
                                                    t=x+y;
                                                    C.str2.setLength(0);
                                                    C.JText.setText(String.valueOf(t));
                                                    C.str1.setLength(0);
                                                    C.str1.append(String.valueOf(t));
                                                    C.v=0;
                                                    C.z=0;
                                                    break;
                                            case 2:
                                                x=Double.parseDouble(C.str1.toString());
                                                    y=Double.parseDouble(C.str2.toString());
                                                    t=x-y;
                                                    C.str2.setLength(0);
                                                    C.JText.setText(String.valueOf(t));
                                                    C.str1.setLength(0);
                                                    C.str1.append(String.valueOf(t));
                                                    C.v=0;
                                                    C.z=0;
                                                    break;
                                            case 3:
                                                    x=Double.parseDouble(C.str1.toString());
                                                    y=Double.parseDouble(C.str2.toString());
                                                    t=x*y;
                                                    C.str2.setLength(0);
                                                    C.JText.setText(String.valueOf(t));
                                                    C.str1.setLength(0);
                                                    C.str1.append(String.valueOf(t));
                                                    C.v=0;
                                                    C.z=0;
                                                    break;
                                            case 4:
                                                    x=Double.parseDouble(C.str1.toString());
                                                    y=Double.parseDouble(C.str2.toString());
                                                    t=x/y;
                                                    C.str2.setLength(0);
                                                    C.JText.setText(String.valueOf(t));
                                                    C.str1.setLength(0);
                                                    C.str1.append(String.valueOf(t));
                                                    C.v=0;
                                                    C.z=0;
                                                    break;
                                  }
                                  C.bool=false;        
                                  C.flag=5;    
                                  C.m=0;
                                  C.z=0;
                          }   
                        }                    
              }
                catch(Exception ee)
                {
                        ee.printStackTrace();
                }               
        }     
}
收到的鲜花
  • sunkaidong2008-03-19 15:01 送鲜花  6朵  
搜索更多相关主题的帖子: 计算器 Javapet null import StringBuffer 
2008-03-19 14:06
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
得分:0 
这个计算器很不错啊~~~~顶一个,就是在
class monitor implements ActionListener
{
      Counter C;
      Double t=0.0;
      Double x=0.0,y=0.0;
}
这里把Double改成double就可以了
再顶下~~~~~

I'm here, as always...
2008-03-19 14:32
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
呵呵..ls可以写聊天室了..把socket在弄弄..java问题就不大了..帮你顶了..

学习需要安静。。海盗要重新来过。。
2008-03-19 14:37
Javapet
Rank: 1
等 级:禁止访问
帖 子:80
专家分:0
注 册:2008-1-5
收藏
得分:0 
谢谢大家的支持,你们的肯定是我前进的动力,以后我还要向你们学习学习!
2008-03-19 15:20
梦自在
Rank: 1
等 级:新手上路
帖 子:110
专家分:0
注 册:2007-3-26
收藏
得分:0 
代码够长,高手啊。对我这初学者没的帮助啊,头都看昏了~!!!

http://blog./adreamstar/
2008-03-19 18:40
Alistair001
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2008-2-24
收藏
得分:0 
我也想编个计算器,学习了
顶一下!
2008-03-19 19:52
谁与争疯
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:海南省
等 级:版主
威 望:191
帖 子:15071
专家分:17513
注 册:2007-4-22
收藏
得分:0 
哇,滚动完你这个程序都用了N分钟!更何况要看!!!
我从来就没写过这么夸张,这么长!的程序,上帝...

论坛是我家灌水靠大家
2008-03-19 20:40
快速回复:Javapet版的计算器!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033631 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved