⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 counter.java

📁 简单的java计算器 就简单实现+,-,*,/
💻 JAVA
字号:
/*
 * Counter.java
 *
 * Created on 2008年1月1日, 下午5:56
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package calculator;
/**
 *
 * @author Administrator
 */

import java.awt.*;    //这里面有需要的各种组件,例如按钮.....
import java.awt.event.*;   //这里面就是各种组件对应的事件

public class Counter extends WindowAdapter implements ActionListener
{
    double dResult = 0;  //记录输入结果
    double dNowInput = 0;  //记录当前输入的数字
    int Num = 0;   //记录小数位数
    int nOperation = 1;  //记录运算符的类型,=,+,-,*,/...
    int nBitsNum = 0;  //记录总共输入的位数
    boolean isAlreadyHaveDot = false;   //是否已经有小数点了?
    boolean keyAvailable = true;  //按键是否处于有效状态
    boolean isAlreadyClickedEqueal = false;  //是否按下过“=”
    boolean isTempNowInput = false;  //是否计算结果后直接按运算符将结果付给当前输入值
    
    //显示屏即计算器窗口
    Frame frame;
    //计算器上面的所有组件
    Panel p1,p2,p3,p4;
    TextField textF1,textF2;
    Button bOne,bTwo,bThree,bFour,bFive,bSix,bSeven,bEight,bNine,bZero;
    Button bDiv,bSqrt,bMulti,bMinus,bPercent,bPlus,bReciprocal,bEqual,bDot,bNegative;
    Button bBackspace,bCE,bC;
    
    /** Creates a new instance of Counter */
    //构造函数,里面暂时没有东西
    public Counter() 
    {
    }
    
    //用来放计算器上各种组件的,可以多种情况
    private void DisplayPanel()
    {
        p1 = new Panel(new GridLayout(1,3,5,5)); //用于存放Backspace,CE,C键
        p2 = new Panel(new GridLayout(4,1,5,5)); //用于存放数字键及其他键
        p3 = new Panel(new FlowLayout());  //存放p1,p2的
        p4 = new Panel(new FlowLayout());
        //p6 = new Panel(new FlowLayout());
        p3.add(p1);
        p3.add(p2);
    }
    
    //显示数字输入区域
    private void DisplayNumEdit()
    {
        textF1 = new TextField(35);
        textF1.setText("0.");
        textF1.setEditable(false);
        p4.add(textF1);
    }
    //显示数字按钮
    private void DisplayNumButton()
    {
        bOne = new Button("1");
        bTwo = new Button("2");
        bThree = new Button("3");
        bFour = new Button("4");
        bFive = new Button("5");
        bSix = new Button("6");
        bSeven = new Button("7");
        bEight = new Button("8");
        bNine = new Button("9");
        bZero = new Button("0");
        
        bOne.addActionListener(this);
        bTwo.addActionListener(this);
        bThree.addActionListener(this);
        bFour.addActionListener(this);
        bFive.addActionListener(this);
        bSix.addActionListener(this);
        bSeven.addActionListener(this);
        bEight.addActionListener(this);
        bNine.addActionListener(this);
        bZero.addActionListener(this);
    }
    //显示各种符号按钮
    private void DisplayOperationButton()
    {
        bDiv = new Button("/");
        bSqrt = new Button("sqrt");
        bMulti = new Button("*");
        bMinus = new Button("-");
        bPercent = new Button("%");
        bPlus = new Button("+");
        bReciprocal = new Button("1/x");
        bEqual = new Button("=");
        bDot = new Button(".");
        bNegative = new Button("+/-");
        
        bDiv.addActionListener(this);
        bSqrt.addActionListener(this);
        bMulti.addActionListener(this);
        bMinus.addActionListener(this);
        bPercent.addActionListener(this);
        bPlus.addActionListener(this);
        bReciprocal.addActionListener(this);
        bEqual.addActionListener(this);
        bDot.addActionListener(this);
        bNegative.addActionListener(this);
    }
    
    private void DisplayOtherButton()
    {
        bBackspace = new Button("Backspace");
        bCE = new Button("CE");
        bC = new Button("C");
        
        bBackspace.addActionListener(this);
        bCE.addActionListener(this);
        bC.addActionListener(this);
    }
    
    //把所有的组件添加到计算器窗口
    private void AddAllControl()
    {
        p2.add(bSeven);
        p2.add(bEight);
        p2.add(bNine);
        p2.add(bDiv);
        p2.add(bSqrt);
        p2.add(bFour);
        p2.add(bFive);
        p2.add(bSix);
        p2.add(bMulti);
        p2.add(bPercent);
        p2.add(bOne);
        p2.add(bTwo);
        p2.add(bThree);
        p2.add(bMinus);
        p2.add(bReciprocal);        
        p2.add(bZero);
        p2.add(bNegative);
        p2.add(bDot);
        p2.add(bPlus);
        p2.add(bEqual);
        
        p1.add(bBackspace);
        p1.add(bCE);
        p1.add(bC);
    }
    
    //布局所有的组件,以像计算器
    public void Display()
    {
        frame = new Frame("计算器");  //产生窗口
        frame.setSize(280,230);   //设置窗口大小
        frame.setLocation(200,200);  
        frame.setBackground(Color.LIGHT_GRAY);  //输入区背景颜色
        frame.setResizable(false);        
        frame.setLayout(new BorderLayout(3,3));  
        
        DisplayPanel();
        
        DisplayNumEdit();
        
        frame.add(p4,BorderLayout.NORTH);
        frame.add(p3,BorderLayout.CENTER);
        //frame.add(p3,BorderLayout.WEST);
       
        DisplayNumButton();
        
        DisplayOperationButton();
        
        DisplayOtherButton();
        
        AddAllControl();
        
        frame.setVisible(true);
        frame.addWindowListener(this);
    }
    
    public void actionPerformed(ActionEvent event)
    {
        //输入0--9的数字
        if (this.keyAvailable && event.getActionCommand().length() == 1 &&
            event.getActionCommand().compareTo("0") >= 0 && 
            event.getActionCommand().compareTo("9") <= 0)
        {
            if (this.isTempNowInput)
            {
                this.dNowInput = 0;
                this.isTempNowInput = false;
            }
            this.nBitsNum++;
            if (this.isAlreadyHaveDot == false)
                this.dNowInput = this.dNowInput * 10 +
                                  Double.parseDouble(event.getActionCommand());
            else
            {
                double TempNum = Double.parseDouble(event.getActionCommand());
                for (int i = this.Num;i < 0;i++)
                {
                    TempNum *= 0.1;
                }
                this.dNowInput += TempNum;
                this.Num--;
            }
            this.textF1.setText(Double.toString(this.dNowInput));
        }
        //输入小数点
        if (this.keyAvailable && event.getActionCommand() == ".")
        {
            if (this.isAlreadyHaveDot == false)
            {
                this.nBitsNum++;
                this.isAlreadyHaveDot = true;
                this.Num = -1;
            }
        }
        //输入+,-,*,/
        if (this.keyAvailable && event.getActionCommand() == "+" ||
            event.getActionCommand() == "-" || 
            event.getActionCommand() == "*" ||
            event.getActionCommand() == "/")
        {
            if (this.isAlreadyClickedEqueal)
            {
                this.dNowInput = this.dResult;
                this.isTempNowInput = true;
            }
            else
            {
                switch (this.nOperation)
                {
                    case 1: this.dResult += this.dNowInput; break;
                    case 2: this.dResult -= this.dNowInput; break;
                    case 3: this.dResult *= this.dNowInput; break;
                    case 4:
                    {
                        if (this.dNowInput == 0)
                        {
                            textF1.setText("除数不能为0");
                            this.keyAvailable = false;
                        }
                        else
                            this.dResult = this.dResult / this.dNowInput;
                    }
                }
                if (this.keyAvailable)
                    textF1.setText(Double.toString(this.dResult));
                this.dNowInput = 0;
            }
            
            if (event.getActionCommand() == "+")
                this.nOperation = 1;
            if (event.getActionCommand() == "-")
                this.nOperation = 2;
            if (event.getActionCommand() == "*")
                this.nOperation = 3;
            if (event.getActionCommand() == "/")
                this.nOperation = 4;
            
            this.nBitsNum = 0;
            this.isAlreadyClickedEqueal = false;
        }
        //正负号
        if (this.keyAvailable && event.getActionCommand() == "+/-")
        {
            this.dNowInput = 0 - this.dNowInput;
            textF1.setText(Double.toString(this.dNowInput));
        }
        //C键,全部清除
        if (event.getActionCommand() == "C")
        {
            this.nBitsNum = 0;
            this.dResult = 0;
            this.dNowInput = 0;
            this.isAlreadyHaveDot = false;
            this.Num = 0;
            this.nOperation = 1;
            this.keyAvailable = true;
            this.isAlreadyClickedEqueal = false;
            textF1.setText("0.");
        }
        //CE键,清除刚输入的数
        if (event.getActionCommand() == "CE")
        {
            this.nBitsNum = 0;
            this.dNowInput = 0;
            this.isAlreadyHaveDot = false;
            this.Num = 0;
            this.nOperation = 1;
            this.keyAvailable = true;
            textF1.setText("0.");
        }
        //开平方,Sqrt键
        if (this.keyAvailable && event.getActionCommand() == "sqrt")
        {
            if (this.isAlreadyClickedEqueal)
            {
                if (this.dResult >= 0)
                {
                    this.dResult = Math.sqrt(this.dResult);
                    textF1.setText(Double.toString(this.dResult));
                }
                else
                {
                    textF1.setText("函数输入无效");
                    this.keyAvailable = false;
                }
            }
            else
            {
                if (this.dNowInput >= 0) 
                {
                    this.dNowInput = Math.sqrt(this.dNowInput);
                    textF1.setText(Double.toString(this.dNowInput));
                }
                else
                {
                    textF1.setText("函数输入无效");
                    this.keyAvailable = false;
                }    
            }
        }
        //1/x键
        if (this.keyAvailable && event.getActionCommand() == "1/x")
        {
            if (this.dNowInput == 0)
            {
                textF1.setText("除数不能为零");
                this.keyAvailable = false;
            }
            else
            {
                this.dNowInput = 1 / this.dNowInput;
                textF1.setText(Double.toString(this.dNowInput));
            }
        }
        //等号键,=
        if (this.keyAvailable && event.getActionCommand() == "=")
        {
            this.isAlreadyClickedEqueal = true;
            switch (this.nOperation)
            {
                case 1: this.dResult += this.dNowInput; break;
                case 2: this.dResult -= this.dNowInput; break;
                case 3: this.dResult *= this.dNowInput; break;
                case 4:
                {
                    if (this.dNowInput == 0)
                    {
                        textF1.setText("除数不能为0");
                        this.keyAvailable = false;
                    }
                    else
                        this.dResult = this.dResult / this.dNowInput;
                }
            }
            if (this.keyAvailable)
                textF1.setText(Double.toString(this.dResult));
        }
        //%号
        if (this.keyAvailable && event.getActionCommand() == "%")
        {
            this.dNowInput = (this.dResult * this.dNowInput) / 100;
            textF1.setText(Double.toString(this.dNowInput));
        }
        //BackSpace键
        if (this.keyAvailable && event.getActionCommand() == "Backspace")
        {
            if (!this.isAlreadyClickedEqueal)
            {
                if (this.dNowInput != 0)
                {
                    if (this.isAlreadyHaveDot)
                    {
                        if (this.Num == -1)
                        {
                            this.isAlreadyHaveDot = false;
                            this.Num = 0;
                        }
                        else
                        {
                            String Str,Str1;
                            Str = textF1.getText();
                            Str1 = Str.substring(0,this.nBitsNum-1);
                            this.nBitsNum--;
                            this.Num++;
                            this.dNowInput = Double.parseDouble(Str1);
                            textF1.setText(Double.toString(this.dNowInput));
                        }
                    }
                    else
                    {
                        int TempNum;
                        TempNum = (int)(this.dNowInput / 10);
                        this.dNowInput = (double)TempNum;
                        textF1.setText(Double.toString(this.dNowInput));
                    }
                }
            }
        }
    }
    
    /**
     * @param args the command line arguments
     */
    //主函数
    public static void main(String[] args) 
    {
        // TODO code application logic here
        //产生一个计算器
        Counter counter = new Counter();
        counter.Display();
    }
    
    //关闭窗口
    public void windowClosing(WindowEvent event)
    {
        System.exit(0);
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -