Java 计算器 四则运算 简单计算器

May 3, 2009 | tags Java 计算器   | views
Comments 0

Java 计算器 四则运算 超级简单的计算器

[co de]

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Kevin125
 */

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

class Kevin extends JFrame implements ActionListener
{
    Container contentPane;
    Panel panel;
    JTextField tf;
    JPanel xp;
    JButton btnclose;
    JPanel cp;
    JButton btn7;
    JButton btn8;
    JButton btn9;
    JButton btndiv;
    JButton btn4;
    JButton btn5;
    JButton btn6;
    JButton btnmuti;
    JButton btn1;
    JButton btn2;
    JButton btn3;
    JButton btnminu;
    JButton btn0;
    JButton btndot;
    JButton btnclear;
    JButton btnplu;
    JButton btnequ;
    StringBuffer temp;
    double result=0;
    Kevin(String str)
    {
        super(str);
        setBounds(100,200,200,100);
        setSize(360,220);
        setVisible(true);
        setResizable(false);
        contentPane=this.getContentPane();
        panel=new Panel();
        GridLayout grid2=new GridLayout(1,2);
        panel.setLayout(grid2);
        tf=new JTextField(12);
        tf.setHorizontalAlignment(JTextField.RIGHT);
        tf.addActionListener(this);
        panel.add(tf);
        xp=new JPanel();
        btnclose=new JButton("关闭");
        btnclose.addActionListener(this);
        btnclear=new JButton("Clear");
        btnclear.addActionListener(this);
        xp.add(btnclear);
        xp.add(btnclose);
        panel.add(xp);
        contentPane.add(panel,BorderLayout.NORTH);
        cp=new JPanel();
        GridLayout grid=new GridLayout(5,5);
        cp.setLayout(grid);
        btn7=new JButton("7");
        btn7.addActionListener(this);
        cp.add(btn7);
        btn8=new JButton("8");
        btn8.addActionListener(this);
        cp.add(btn8);
        btn9=new JButton("9");
        btn9.addActionListener(this);
        cp.add(btn9);
        btndiv=new JButton("/");
        btndiv.addActionListener(this);
        cp.add(btndiv);
        btn4=new JButton("4");
        btn4.addActionListener(this);
        cp.add(btn4);
        btn5=new JButton("5");
        btn5.addActionListener(this);
        cp.add(btn5);
        btn6=new JButton("6");
        btn6.addActionListener(this);
        cp.add(btn6);
        btnmuti=new JButton("*");
        btnmuti.addActionListener(this);
        cp.add(btnmuti);
        btn1=new JButton("1");
        btn1.addActionListener(this);
        cp.add(btn1);
        btn2=new JButton("2");
        btn2.addActionListener(this);
        cp.add(btn2);
        btn3=new JButton("3");
        btn3.addActionListener(this);
        cp.add(btn3);
        btnminu=new JButton("-");
        btnminu.addActionListener(this);
        cp.add(btnminu);
        btn0=new JButton("0");
        btn0.addActionListener(this);
        cp.add(btn0);
        btndot=new JButton(".");
        btndot.addActionListener(this);
        cp.add(btndot);
        btnequ=new JButton("=");
        btnequ.addActionListener(this);
        cp.add(btnequ);
        btnplu=new JButton("+");
        btnplu.addActionListener(this);
        cp.add(btnplu);
        cp.validate();
        contentPane.add(cp,BorderLayout.CENTER);
        contentPane.validate();
        validate();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        temp=new StringBuffer("");
    }
    public void actionPerformed(ActionEvent e)
    {
     try{
     if(e.getSource()==btnclose)
     {
         System.exit(0);
     }
     else if(e.getSource()==btn7)
     {
         temp.append(btn7.getText());
         String str1=temp.toString();
         tf.setText(str1);
     }
     else if(e.getSource()==btn8)
     {
         temp.append(btn8.getText());
         String str2=temp.toString();
         tf.setText(str2);
     }
     else if(e.getSource()==btn9)
     {
         temp.append(btn9.getText());
         String str3=temp.toString();
         tf.setText(str3);
     }
     else if(e.getSource()==btndiv)
     {
         temp.append(btndiv.getText());
         String str4=temp.toString();
         tf.setText(str4);
     }
     else if(e.getSource()==btn4)
     {
         temp.append(btn4.getText());
         String str6=temp.toString();
         tf.setText(str6);
     }
     else if(e.getSource()==btn5)
     {
         temp.append(btn5.getText());
         String str7=temp.toString();
         tf.setText(str7);
     }
     else if(e.getSource()==btn6)
     {
         temp.append(btn6.getText());
         String str8=temp.toString();
         tf.setText(str8);
     }
     else if(e.getSource()==btnmuti)
     {
         temp.append(btnmuti.getText());
         String str9=temp.toString();
         tf.setText(str9);
     }
     else if(e.getSource()==btn1)
     {
         temp.append(btn1.getText());
         String str11=temp.toString();
         tf.setText(str11);
     }
     else if(e.getSource()==btn2)
     {
         temp.append(btn2.getText());
         String str12=temp.toString();
         tf.setText(str12);
     }
     else if(e.getSource()==btn3)
     {
         temp.append(btn3.getText());
         String str13=temp.toString();
         tf.setText(str13);
     }
     else if(e.getSource()==btnminu)
     {
         temp.append(btnminu.getText());
         String str14=temp.toString();
         tf.setText(str14);
     }
     else if(e.getSource()==btn0)
     {
         temp.append(btn0.getText());
         String str16=temp.toString();
         tf.setText(str16);
     }
     else if(e.getSource()==btndot)
     {
         temp.append(btndot.getText());
         String str17=temp.toString();
         tf.setText(str17);
     }
     else if(e.getSource()==btnclear)
     {
         temp.delete(0,temp.length());
         tf.setText("0.0");
     }
     else if(e.getSource()==btnplu)
     {
         temp.append(btnplu.getText());
         String str19=temp.toString();
         tf.setText(str19);
     }
     else if(e.getSource()==btnequ)
     {
         String str=temp.toString();
         char[] c=str.toCharArray();
         temp.delete(0,temp.length());
         int len=c.length;
         for(int i=0;i<8;i++)
         {
             if(c[i]=='+')
             {
                 double temp1=Double.parseDouble(str.substring(0,i));
                 double temp2=Double.parseDouble(str.substring(i+1,len));
                 result=temp1+temp2;
                 temp.append(String.valueOf(result));
                 tf.setText(temp.toString());
                 break;
             }
             if(c[i]=='-')
             {
                 double temp1=Double.parseDouble(str.substring(0,i));
                 double temp2=Double.parseDouble(str.substring(i+1,len));
                 result=temp1-temp2;
                 temp.append(String.valueOf(result));
                 tf.setText(temp.toString());
                 break;
             }
             if(c[i]=='*')
             {
                 double temp1=Double.parseDouble(str.substring(0,i));
                 double temp2=Double.parseDouble(str.substring(i+1,len));
                 result=temp1*temp2;
                 temp.append(String.valueOf(result));
                 tf.setText(temp.toString());
                 break;
             }
             if(c[i]=='/')
             {
                 double temp1=Double.parseDouble(str.substring(0,i));
                 double temp2=Double.parseDouble(str.substring(i+1,len));
                 if(temp2!=0)
                 {
                     result=temp1/temp2;
                     temp.append(String.valueOf(result));
                     tf.setText(temp.toString());
                     break;
                 }
                 else
                 {
                     tf.setText("除数为零,请重新输入!");
                 }
             }
         }
     }
     }catch(Exception ee)
     {
         tf.setText("ERROR");
     }
     }
}
public class NewCalc
{
    public static void main(String[] args)
    {
        Kevin k=new Kevin("计算器");
    }
}

[/co de]


    相关文章:



发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。