zoukankan      html  css  js  c++  java
  • 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){}
            }  
        }
    }

    运行结果:

    还是比较好看的,菜单还有快捷键还可以作为补充添加上去就比较仿windows计算器了。

  • 相关阅读:
    Open Auth辅助库(使用ImitateLogin实现登录)
    dotNet使用HttpWebRequest模拟浏览器
    开源一个社交网站模拟登录的库
    Storm系列(三):创建Maven项目打包提交wordcount到Storm集群
    Storm系列(二):使用Csharp创建你的第一个Storm拓扑(wordcount)
    Storm系列(一):搭建dotNet开发Storm拓扑的环境
    单点登录原理与简单实现
    策略模式Strategy
    MySQL 主从复制
    Windows下搭建MySql Master-Master Replication
  • 原文地址:https://www.cnblogs.com/UUUP/p/3812593.html
Copyright © 2011-2022 走看看