Java Implementation simple Calculator function

Source: Internet
Author: User
Tags gettext

Children's shoes, is not the use of calculators, but also to enter the run, Input calc, too troublesome, and sometimes even forget how to spell the word, hehe
programmers write their own code implementation, and simple, and convenient AH

The following is the code (you might want to build an executable tool for reference:http://www.cnblogs.com/holdon521/p/4483966.html)

 Packagecom;Importjava.awt.BorderLayout;ImportJava.awt.EventQueue;ImportJava.awt.Font;Importjava.awt.GridLayout;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel;ImportJavax.swing.JTextField;Importjavax.swing.SwingConstants;ImportJavax.swing.UIManager;ImportJavax.swing.border.EmptyBorder; Public classTestextendsjframe{/*** Humphrey*/  Private Static Final LongSerialversionuid = -9075562467166618473l; PrivateJPanel ContentPane; PrivateJTextField display; PrivateActionListener Insert =Newinsertaction (); PrivateActionListener Command =Newcommandaction (); Private Doubleresult = 0; PrivateString Lastcommand = "="; Private BooleanStart =true; /*** Launch the application. */   Public Static voidMain (string[] args) {Try{Uimanager.setlookandfeel ("Com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } Catch(Throwable e) {e.printstacktrace (); } eventqueue.invokelater (NewRunnable () { Public voidrun () {Try{Test frame=NewTest (); Frame.setvisible (true); } Catch(Exception e) {e.printstacktrace ();  }          }      }); }    /*** Create the frame. */   PublicTest () {Settitle ("\u8ba1\u7b97\u5668");      Setdefaultcloseoperation (Jframe.exit_on_close); Setlocationbyplatform (true); ContentPane=NewJPanel (); Contentpane.setborder (NewEmptyborder (5, 5, 5, 5)); Contentpane.setlayout (NewBorderLayout (0, 0));            Setcontentpane (ContentPane); JPanel DisplayPanel=NewJPanel ();            Contentpane.add (DisplayPanel, Borderlayout.north); Display=NewJTextField (); Display.settext ("0");      Display.sethorizontalalignment (Swingconstants.right); Display.seteditable (false); Display.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));      Displaypanel.add (display); Display.setcolumns (13); JPanel Buttonpanel=NewJPanel ();      Contentpane.add (Buttonpanel, Borderlayout.center); Buttonpanel.setlayout (NewGridLayout (4, 4, 5, 5)); JButton Number7button=NewJButton ("7");      Number7button.addactionlistener (insert); Number7button.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));            Buttonpanel.add (Number7button); JButton Number8button=NewJButton ("8");      Number8button.addactionlistener (insert); Number8button.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));            Buttonpanel.add (Number8button); JButton Number9button=NewJButton ("9");      Number9button.addactionlistener (insert); Number9button.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));            Buttonpanel.add (Number9button); JButton Dividebutton=NewJButton ("/");      Dividebutton.addactionlistener (command); Dividebutton.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));            Buttonpanel.add (Dividebutton); JButton Number4button=NewJButton ("4");      Number4button.addactionlistener (insert); Number4button.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));            Buttonpanel.add (Number4button); JButton Number5button=NewJButton ("5");      Number5button.addactionlistener (insert); Number5button.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));            Buttonpanel.add (Number5button); JButton Number6button=NewJButton ("6");      Number6button.addactionlistener (insert); Number6button.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));            Buttonpanel.add (Number6button); JButton Multiplybutton=NewJButton ("*");      Multiplybutton.addactionlistener (command); Multiplybutton.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));            Buttonpanel.add (Multiplybutton); JButton Number3button=NewJButton ("1");      Number3button.addactionlistener (insert); Number3button.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));            Buttonpanel.add (Number3button); JButton Number2button=NewJButton ("2");      Number2button.addactionlistener (insert); Number2button.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));            Buttonpanel.add (Number2button); JButton Number1button=NewJButton ("3");      Number1button.addactionlistener (insert); Number1button.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));            Buttonpanel.add (Number1button); JButton Subtractbutton=NewJButton ("-");      Subtractbutton.addactionlistener (command); Subtractbutton.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));            Buttonpanel.add (Subtractbutton); JButton Number0button=NewJButton ("0");      Number0button.addactionlistener (insert); Number0button.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));            Buttonpanel.add (Number0button); JButton Dotbutton=NewJButton ("."); Dotbutton.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));            Buttonpanel.add (Dotbutton); JButton Equalbutton=NewJButton ("=");      Equalbutton.addactionlistener (command); Equalbutton.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));            Buttonpanel.add (Equalbutton); JButton AddButton=NewJButton ("+");      Addbutton.addactionlistener (command); Addbutton.setfont (NewFont ("Microsoft Jas Black", Font.plain, 15));      Buttonpanel.add (AddButton);  Pack (); }    Private classInsertactionImplementsActionListener { Public voidactionperformed (ActionEvent e) {String input=E.getactioncommand (); String text=Display.gettext (); if(start) {Display.settext (""); Start=false; }          if(Text.startswith (".") {Display.settext ("0" + display.gettext () +input); } Else if(Text.startswith (" -0.") | | Text.startswith ("0.")) {Display.settext (Display.gettext () )+input); } Else if(Text.startswith ("-0")) {Display.settext ("-" +input); } Else if(Text.startswith ("0") {display.settext (input); } Else{Display.settext (Display.gettext ()+input); }      }  }    Private classCommandactionImplementsActionListener { Public voidactionperformed (ActionEvent e) {String command=E.getactioncommand (); if(start) {if(Command.equals ("-") {display.settext (command); Start=false; } Else{Lastcommand=command; }          } Else{Calculate (double.parsedouble (Display.gettext ())); Lastcommand=command; Start=true; }      }  }     Public voidCalculateDoublex) {Charoperator = Lastcommand.charat (0); Switch(operator) { Case+: Result+=x;  Break;  Case‘-‘: Result-=x;  Break;  Case‘*‘: Result*=x;  Break;  Case‘/‘: Result/=x;  Break;  Case=: Result=x;  Break; } display.settext ("" +result); }}

Java Implementation simple Calculator function

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.