A very simple calculator

Source: Internet
Author: User

Today, I was asked to write a calculator on the iPhone. After all, I was not very familiar with it. I spent an hour writing a calculator in Java. It was very easy to go through the next process. The Code is as follows:

/* * To change this template, choose Tools | Templates * and open the template in the editor. */package calculator;import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;/** * * @author GL */public class Calculator extends JFrame{    /**     * @param args the command line arguments     */        int first = 0;    int second = 0;    int select = 0;    int counter = 0;    input in;    JLabel out = new JLabel("0");        public static void main(String[] args) {        // TODO code application logic here        JFrame j = new Calculator();    }        public Calculator(){        in = new input();        setLayout(new BorderLayout());        add(in,BorderLayout.CENTER);        add(out,BorderLayout.NORTH);        this.setVisible(true);        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        this.setTitle("Calculator");           }        class input extends JPanel{        public input(){            setLayout(new GridLayout(5,4));            JButton[] enter = new JButton[20];                        enter[0] = new JButton("c");            enter[1] = new JButton("<-");            enter[2] = new JButton("/");            enter[3] = new JButton("*");            enter[4] = new JButton("7");            enter[5] = new JButton("8");            enter[6] = new JButton("9");            enter[7] = new JButton("-");            enter[8] = new JButton("4");            enter[9] = new JButton("5");            enter[10] = new JButton("6");            enter[11] = new JButton("+");            enter[12] = new JButton("1");            enter[13] = new JButton("2");            enter[14] = new JButton("3");            enter[15] = new JButton("=");            enter[16] = new JButton("0");            enter[17] = new JButton(".");            enter[18] = new JButton("+/-");            enter[19] = new JButton("f");                        enter[0].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                    first = 0;                    second = 0;                    select = 0;                    out.setText("0");                }            });                        enter[1].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    throw new UnsupportedOperationException("Not supported yet.");                }            });                         enter[2].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                    select = 4;                    counter = 0;                    out.setText("/");                }            });                          enter[3].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                   select = 3;                   counter = 0;                   out.setText("*");                }            });                           enter[4].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                   if(select == 0){                       first = 7 + (int)(first*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(first));                           }                   else{                       second = 7 + (int)(second*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(second));                   }                }            });                            enter[5].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                     if(select == 0){                       first = 8 + (int)(first*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(first));                           }                   else{                       second = 8 + (int)(first*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(second));                   }                }            });                             enter[6].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                     if(select == 0){                       first = 9 + (int)(first*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(first));                           }                   else{                       second = 9 + (int)(second*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(second));                   }                }            });                              enter[7].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                   select = 2;                   counter = 0;                   out.setText("-");                }            });                               enter[8].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                     if(select == 0){                       first = 4 + (int)(first*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(first));                           }                   else{                       second = 4 + (int)(second*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(second));                   }                }            });                                enter[9].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                     if(select == 0){                       first = 5 + (int)(first*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(first));                           }                   else{                       second = 5 + (int)(first*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(second));                   }                }            });                                 enter[10].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                     if(select == 0){                       first = 6 + (int)(first*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(first));                           }                   else{                       second = 6 + (int)(second*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(second));                   }                }            });                                  enter[11].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                   select = 1;                   counter = 0;                   out.setText("+");                }            });                                   enter[12].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                     if(select == 0){                       first = 1 + (int)(first*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(first));                           }                   else{                       second = 1 + (int)(second*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(second));                   }                }            });                                    enter[13].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                   if(select == 0){                       first = 2 + (int)(first*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(first));                           }                   else{                       second = 2 + (int)(second*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(second));                   }                }            });                                     enter[14].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                   if(select == 0){                       first = 3 + (int)(first*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(first));                           }                   else{                       second = 3 + (int)(second*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(second));                   }                }            });                                      enter[15].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                   switch(select){                       case 1:                           out.setText(String.valueOf(first+second));                           break;                       case 2:                            out.setText(String.valueOf(first-second));                           break;                       case 3:                            out.setText(String.valueOf(first*second));                           break;                       case 4:                            out.setText(String.valueOf(first/second));                           break;                   }                                      first = 0;                   second = 0;                   select = 0;                   counter = 0;                }            });                                       enter[16].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                   if(select == 0){                       first = 0 + (int)(first*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(first));                           }                   else{                       second = 0 + (int)(second*Math.pow(10, counter));                       counter++;                       out.setText(String.valueOf(second));                   }                }            });                                        enter[17].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                                   }            });                                         enter[18].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                   if(select == 0){                                          first = 0 -first;                       out.setText(String.valueOf(first));                   }                                      else {                       second = 0- second;                       out.setText(String.valueOf(second));                   }                }            });                                          enter[19].addActionListener(new ActionListener( ) {                @Override                public void actionPerformed(ActionEvent e) {                    //throw new UnsupportedOperationException("Not supported yet.");                                   }            });                                          for(int i = 0; i < 20; i++){                add(enter[i]);            }                    }    }}

The final running result is:

The interface is ugly. I also feel ugly. It doesn't matter. I tested it a little and it should be okay.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.