Java implements a simple Calculator class instance _java

Source: Internet
Author: User
Tags mul

The examples in this article describe the Java implementation of a simple calculator class. Share to everyone for your reference. as follows:

Package chap;
Import Java.awt.BorderLayout;
Import Java.awt.Color;
Import Java.awt.FlowLayout;
Import Java.awt.GridLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.text.DecimalFormat;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;
Import Javax.swing.JTextField;
  public class Calculator {private JFrame frame;
  Private JPanel Panel,panelkeys,panelkeys_up,panelkeys_down; Private JTextField textcomputer;//Computing area private JButton buttonbk,buttonc;//backspace and clear 0 keys private JButton button[];//Digital key Group PR Ivate JButton buttondot,buttonaddsub,buttonadd,buttonsub,buttonmul,buttondiv,button1,button2,button3,buttonequal
  ;
  Private double result;//calculation result private final short ADD = 1;
  Private final short SUB = 2;
  Private final short MUL = 3;
  Private final short DIV = 4;
    Private short operator = -1;//operator Public Calculator () {frame = new JFrame ("Computer");
    Frame.setsize (400, 250);
  panel = new JPanel ()//Global panel  Panel.setvisible (TRUE);
    Frame.setvisible (TRUE);
    Frame.getcontentpane (). Add (panel);
    ActionListener listener = new Computeractionlistener ();/key listener//calculation Area Textcomputer = new JTextField (15);
    Textcomputer.settext ("");
    Textcomputer.seteditable (FALSE);
    Textcomputer.setbackground (New Color (255,255,255));
    Function key upper Half panelkeys_up = new JPanel ();
    Panelkeys_up.setlayout (New FlowLayout (flowlayout.right));
    BUTTONBK = new JButton ("Backspace");
    Buttonbk.setforeground (New Color (255,0,0));
    Buttonc = new JButton ("C");
    Buttonc.setforeground (New Color (255,0,0));
    Buttonbk.addactionlistener (listener);
    Buttonc.addactionlistener (listener);
    Panelkeys_up.add (BUTTONBK);
    Panelkeys_up.add (Buttonc);
    Function key lower part Panelkeys_down = new JPanel ();
    Panelkeys_down.setlayout (new GridLayout (4,5));
    button = new JBUTTON[10];
      for (int i = 0;i < button.length;i++) {Button[i] = new JButton (integer.tostring (i)); button[i].seTforeground (New Color (255,0,0));
    } buttonaddsub = new JButton ("+ +");
    Buttonaddsub.setforeground (New Color (255,0,0));
    Buttonadd = new JButton ("+");
    Buttonadd.setforeground (New Color (255,0,0));
    Buttonsub = new JButton ("-");
    Buttonsub.setforeground (New Color (255,0,0));
    Buttonmul = new JButton ("*");
    Buttonmul.setforeground (New Color (255,0,0));
    Buttondiv = new JButton ("/");
    Buttondiv.setforeground (New Color (255,0,0));
    button1 = new JButton ();
    Button2 = new JButton ();
    Button3 = new JButton ();
    Button1.setforeground (New Color (255,0,0));
    Button2.setforeground (New Color (255,0,0));
    Button3.setforeground (New Color (255,0,0));
    buttonequal = new JButton ("=");
    Buttonequal.setforeground (New Color (255,0,0));
    Buttonaddsub.addactionlistener (listener);
    Buttonadd.addactionlistener (listener);
    Buttonsub.addactionlistener (listener);
    Buttonmul.addactionlistener (listener);
    Buttondiv.addactionlistener (listener); BUttonequal.addactionlistener (listener);
    for (int i = 0; I <=9 i++) {Button[i].addactionlistener (listener);
    for (int i = 0; I <=9 i++) {panelkeys_down.add (button[i]);
    } panelkeys_down.add (Buttonaddsub);
    Panelkeys_down.add (Buttonadd);
    Panelkeys_down.add (buttonsub);
    Panelkeys_down.add (Buttonmul);
    Panelkeys_down.add (BUTTONDIV);
    Panelkeys_down.add (buttonequal);
    Panel.setlayout (New BorderLayout ());
    Panel.add (Textcomputer,borderlayout.north);
    Panel.add (Panelkeys_up,borderlayout.center);
  Panel.add (Panelkeys_down,borderlayout.south); Class Computeractionlistener implements actionlistener{@Override public void actionperformed (ActionEvent even
      T) {//TODO auto-generated method stub Object Keybutton = Event.getsource ();
      String text = Textcomputer.gettext ();
      DecimalFormat df = new DecimalFormat ("0.###########"); Backspace if (Keybutton = = BUTTONBK && text.lengtH () > 0) {textcomputer.settext (text.substring (0,text.length ()-1));
        //c key if (Keybutton = = buttonc) {result = 0;
      Textcomputer.settext ("");
        }//numeric key for (int i=0;i<10;i++) {if (Keybutton = = Button[i]) {textcomputer.settext (text+i);
      } if (Keybutton = = Buttonadd) {operator = 1;
      } if (Keybutton = = buttonsub) {operator = 2;
      } if (Keybutton = = Buttonmul) {operator = 3;
      } if (Keybutton = = buttondiv) {operator = 4; }//Symbol key if (Keybutton = Buttonadd | | keybutton = = Buttonsub | | keybutton = BUTTONMUL | | keybutton = = Buttondi V | |
          Keybutton = = buttonequal) {switch (operator) {case Add:result + = double.parsedouble (text);
        Break
          Case Sub:result-=double.parsedouble (text);
        Break
          Case Mul:result *=double.parsedouble (text); BreAk
          Case Div:result/=double.parsedouble (text);
        Break
        Default:result = double.parsedouble (text);
      } textcomputer.settext ("");
      } if (Keybutton = = buttonequal) {Textcomputer.settext (string.valueof (result));
  }} public static void Main (String args[]) {new Calculator ();

 }
}

I hope this article will help you with your Java programming.

Related Article

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.