首页 > 代码库 > java简易计算器

java简易计算器

此小程序实现了计算器的基本功能:

import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SimpleCalc extends JFrame{    private static final long serialVersionUID = 1L;    String[] labels = {"←","CE","±","√",                "7","8","9","/",                "4","5","6","*",                "1","2","3","-",                "0",".","=","+"};    JButton[] btn;    JTextField tf;    Double n1 = 0.0, n2 = 0.0;    String opt = "";    SimpleCalc(String name)    {        super(name);        this.setVisible(true);        this.setBounds(400,300,300,400);        this.addWindowListener(new WindowAdapter()        {            public void windowClosing(WindowEvent e)            {                System.exit(0);            }        });                    }    public void init()    {        tf = new JTextField();        tf.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);        tf.setFont(new Font("宋体", 30,20));        btn = new JButton[labels.length];        for(int i=0;i<labels.length;i++)        {            btn[i] = new JButton(labels[i]);            btn[i].setForeground(Color.blue);            //System.out.print(btn[i].getActionCommand()+" ");        }                            JPanel p = new JPanel();        p.setLayout(new GridLayout(5,5,3,3));        for(int i=0;i<labels.length;i++)            p.add(btn[i]);        this.add(BorderLayout.NORTH,tf);        this.add(BorderLayout.CENTER,p);        this.pack();                for(int i=0;i<labels.length;i++)            btn[i].addActionListener(new Monitor());                }        public static void main(String[] args) {        // TODO Auto-generated method stub        new SimpleCalc("java简易计算器").init();    }        class Monitor implements ActionListener    {        @Override        public void actionPerformed(ActionEvent e) {        try{                        String cmd = e.getActionCommand();            if(cmd.equals("0")||cmd.equals("1")||cmd.equals("2")||cmd.equals("3")||cmd.equals("4")||                    cmd.equals("5")||cmd.equals("6")||cmd.equals("7")||cmd.equals("8")||cmd.equals("9"))            {                tf.setText(tf.getText().trim()+e.getActionCommand());                n1 = Double.parseDouble(tf.getText().trim());            }            if(cmd.equals("←"))            {                tf.setText(tf.getText().trim().substring(0,tf.getText().trim().length()-1));            }            else if(cmd.equals("CE"))            {                tf.setText("");            }            else if(cmd.equals("±"))            {                                n1 = Double.parseDouble(tf.getText().trim());                tf.setText(-n1+"");            }            else if(cmd.equals("√"))            {                n1 = Double.parseDouble(tf.getText().trim());                tf.setText(Math.sqrt(n1)+"");            }            else if(cmd.equals("+"))            {                    n2 = Double.parseDouble(tf.getText().trim());                opt = "+";                tf.setText("");            }            else if(cmd.equals("-"))            {                opt = "-";                n2 = Double.parseDouble(tf.getText().trim());                tf.setText("");            }            else if(cmd.equals("*"))            {                opt = "*";                n2 = Double.parseDouble(tf.getText().trim());                tf.setText("");            }            else if(cmd.equals("/"))            {                opt = "/";                n2 = Double.parseDouble(tf.getText().trim());                tf.setText("");            }            else if(cmd.equals("."))            {                if(tf.getText().trim().indexOf(".") != -1) //字符串中已经包含了小数点不做任何操作                {                 }                 else //如果没有小数点                 {                            if(tf.getText().trim().equals("0"))//如果开始为0                    {                        tf.setText(("0"+e.getActionCommand()).toString());                    }                    else if(tf.getText().trim().equals(""))//如果初时显示为空不做任何操作                    {                                }                    else                    {                        tf.setText((tf.getText()+e.getActionCommand()).toString());                    }                }            }            else if(cmd.equals("="))            {                if(opt.equals("+"))                    tf.setText((n2+n1)+"");                else if(opt.equals("-"))                    tf.setText((n2-n1)+"");                else if(opt.equals("*"))                    tf.setText((n2*n1)+"");                else if(opt.equals("/"))                {                    if(n1 == 0)                        tf.setText("除数不能为0");                    else                        tf.setText((n2/n1)+"");                }            }        }catch(Exception ee){}        }      }}

运行结果: