Java Learning--Calculator Applet

Source: Internet
Author: User

Simple Calculator Small program code:

Package jisuanqi;//declares that a packet needs to be inserted import Java.awt.*;import java.lang.object;import java.lang.string;import javax.swing.*; Import Java.awt.event.*;import java.awt.textcomponent;//declares a main class Jisuanqi, continuation window class Framepublic class Jisuanqi extends Frame  {//Declaration of 16 Buttonbutton Anjian1, Anjian2, Anjian3, Anjian4, Anjian5, Anjian6, Anjian7, Anjian8, Anjian9, Anjian10, Anjian11, Anjian12, ANJIAN13, anjian14,anjian15,anjian16;//Declaration text box, used to receive numbers and calculate TextField numtext;//Declarations panel container, for holding various buttons for panel anjianpanel;//constructor Method initializes the button Jisuanqi (String title) {Super (title), Anjian1=new button ("0"), Anjian2=new button ("1"); Anjian3=new button ("2"), Anjian4=new button ("3"), Anjian5=new button ("4"), Anjian6=new button ("5"), Anjian7=new button ( "6"), Anjian8=new button ("7"), Anjian9=new button ("8"), Anjian10=new button ("9"), Anjian11=new button ("+"); anjian12= New Button ("-"); Anjian13=new button ("*"); Anjian14=new button ("/"); Anjian15=new button ("="); anjian16=new button ("Empty" );//Initialize the text box, set the value of the text to "0" numtext=new TextField ("0");//instantiate panel; anjianpanel=new panels ();//Set Button ANJIANPAnel.setlayout (New FlowLayout ());//Instantiate button Anjian1.addactionlistener (new Anjianaction ()); Anjian2.addactionlistener ( New Anjianaction ()); Anjian3.addactionlistener (new Anjianaction ()); Anjian4.addactionlistener (new Anjianaction ()); Anjian5.addactionlistener (New Anjianaction ()); Anjian6.addactionlistener (new Anjianaction ()); Anjian7.addactionlistener (New Anjianaction ()); Anjian8.addactionlistener (new Anjianaction ()); Anjian9.addactionlistener (New Anjianaction ()); Anjian10.addactionlistener (new Anjianaction ()); Anjian11.addactionlistener (New Anjianaction ()); Anjian12.addactionlistener (new Anjianaction ()); Anjian13.addactionlistener (New Anjianaction ()); Anjian14.addactionlistener (new Anjianaction ()); Anjian15.addactionlistener (New Anjianaction ()); Anjian16.addactionlistener (new Anjianaction ());// Add individual buttons to the Panel container Anjianpanel.add (anjian1); Anjianpanel.add (anjian2); Anjianpanel.add (ANJIAN3); Anjianpanel.add ( ANJIAN4); Anjianpanel.add (ANJIAN5); Anjianpanel.add (anjian6); Anjianpanel.add (ANJIAN7); Anjianpanel.add (ANJIAN8); AnJianpanel.add (ANJIAN9); Anjianpanel.add (ANJIAN10); Anjianpanel.add (anjian11); Anjianpanel.add (ANJIAN12); Anjianpanel.add (ANJIAN13); Anjianpanel.add (ANJIAN14); Anjianpanel.add (anjian15); Anjianpanel.add (Anjian16);// Add Event Numtext.addtextlistener (new TextListener () {public void textvaluechanged (TextEvent e) {if (Numtext.gettext ()) for the text box. IndexOf ("0", 0)!=-1) {Numtext.gettext (). Replace ("0", "");}}); Addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent e) {system.exit (0);}}); /Place the text box at the top of Add (Numtext,borderlayout.north);//Add a button (anjianpanel,borderlayout.center);// Sets the text Box property numtext.seteditable (FALSE);} The value of no action int num=0;//numstr is the value of the text box string numstr=null; The Boolean chinage=false;//setting button public int check () {numstr = Numtext.gettext (); if (Numstr.indexof ("+") = = 0) {numstr = Numstr. Replace (' + ', ' 0 '); num = num + integer.parseint (NUMSTR); return num;} else if (Numstr.indexof ("-")! =-1) {numstr = Numstr.replace ("-", "0"); num = Num-integer.parseint (NUMSTR); return num;} else if (Numstr.indexof ("*")!=-1) {nUmstr=numstr.replace (' * ', ' 0 '); num = num * Integer.parseint (NUMSTR); return num;} else if (Numstr.indexof ("/")!=-1) {numstr=numstr.replace ('/', ' 0 '); try{num = Num/integer.parseint (NUMSTR); return num;} catch (ArithmeticException e) {joptionpane.showmessagedialog (null, "divisor cannot be empty!", "Message!", 1); return num;}} else return Num=integer.parseint (NUMSTR);} Create event Private class Anjianaction implements Actionlistener{public void Actionperformed (ActionEvent event) {if ( Event.getactioncommand () = = "0") {if (!numtext.gettext (). Equals ("0")) Numtext.settext (Numtext.gettext () +0);} if (event.getactioncommand () = = "1") Numtext.settext (Numtext.gettext () +1); if (event.getactioncommand () = = "2") Numtext.settext (Numtext.gettext () +2), if (event.getactioncommand () = = "3") Numtext.settext (Numtext.gettext () +3); Event.getactioncommand () = = "4") Numtext.settext (Numtext.gettext () +4); if (event.getactioncommand () = = "5") Numtext.settext (Numtext.gettext () +5), if (event.getactioncommand () = = "6") Numtext.settext (Numtext.gettext () +6); Event.getactioncommand () = = "7") nUmtext.settext (Numtext.gettext () +7), if (event.getactioncommand () = = "8") Numtext.settext (Numtext.gettext () +8); Event.getactioncommand () = = "9") Numtext.settext (Numtext.gettext () +9); if (event.getactioncommand () = = "Empty") { Numtext.settext ("0"); num=0;} if (event.getactioncommand () = = "+") {//call check (); method Check (); Numtext.settext ("+");} if (event.getactioncommand () = = "-") {check (); Numtext.settext ("-");} if (event.getactioncommand () = = "*") {check (); Numtext.settext ("*");} if (event.getactioncommand () = = "/") {if (numstr== "/0") {Joptionpane.showmessagedialog (null, "divisor cannot be empty!", "Message!", 1);} Check (); Numtext.settext ("/");} if (event.getactioncommand () = = "=") {check (); Numtext.settext ("" +num);}}} public static void Main (string[] args) {Jisuanqi jisuanqi=new Jisuanqi ("calculator"); Jisuanqi.setbackground (Color.cyan); Jisuanqi.setsize (300,280);//Set Window size jisuanqi.setlocation (550,350);//Set interface property jisuanqi.setvisible (TRUE);}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Learning--Calculator Applet

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.