Original link: 52317240
Links are explained in detail, as well as examples, the following is the code of the personal reference example experiment.
PackageGUI;Importjava.awt.BorderLayout;Importjava.awt.GridLayout;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.awt.event.KeyAdapter;Importjava.awt.event.KeyEvent;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel;ImportJavax.swing.JTextField;/*** Swing Implements calculator function *@authorChoshengkiang *@versioncreated: May 11, 2018 PM 5:43:35*/ Public classGuidemoextendsJFrameImplementsactionlistener{Private Static Final LongSerialversionuid = 2499964079704437558L; PrivateJTextField result;//text box showing the result of the Operation PrivateJbutton[] buttons;//all the button objects Private FinalString[] characters = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};//all the button text Private BooleanIsfirstdigit =true;//mark the first time you enter a number PrivateString operator = "=";//operator Private DoubleResultnum = 0.0;//result of Operation PublicGuidemo (String title) {//constructors Remember to modify the classes that you write for yourself//title bar Super(title); //sizeSetSize (220, 200); //CenterSetlocationrelativeto (NULL); //Default shutdown Actionsetdefaultcloseoperation (exit_on_close); //prohibit modification of sizeSetresizable (false); //initialize text boxes and buttonsGenerateinterface (); //ShowSetVisible (true); } Public Static voidmain (String [] args) {Guidemo gd=NewGuidemo ("Calculator"); } /*** Initialize text box with button, generate interface*/ Private voidGenerateinterface () {/*text Box*/result=NewJTextField ("0"); //Align Rightresult.sethorizontalalignment (jtextfield.right); //Edit not allowedResult.seteditable (false); //Add a text box to the form NorthAdd (result, Borderlayout.north); /*Button*/Buttons=NewJbutton[characters.length]; JPanel PNL=NewJPanel (NewGridLayout (4, 4, 5, 5)); for(inti = 0; i < buttons.length; i++) {Buttons[i]=NewJButton (Characters[i]); Buttons[i].addactionlistener ( This); Buttons[i].setfocusable (false);//do not allow buttons to position focusPnl.add (Buttons[i]); } //Add all buttons to the middle of the formAdd (PNL, borderlayout.center); //allow content panel to position focus This. Getcontentpane (). Setfocusable (true); //Registering Content Panel event listeners//using adapter implementations This. Getcontentpane (). Addkeylistener (NewKeyadapter () {@Override Public voidkeypressed (KeyEvent e) {Charch = E.getkeychar ();//Get button characters /*working with numbers or operators*/ if('. ' = = CH | | Character.isdigit (CH)) {//Digit decimal pointhandlenumber (string.valueof (ch)); } Else if("+-*/=". INDEXOF (ch)! =-1 | | E.getkeycode () = = 10) {//operatorhandleoperator (string.valueof (ch)); } Else if(E.getkeycode () = = 8) {//BACKSPACE BarString tmp =Result.gettext (); if(tmp.length () = = 1) {Result.settext ("0"); Isfirstdigit=true; } Else{result.settext (tmp.substring (0, Tmp.length ()-1)); } } } }); } @Override Public voidactionperformed (ActionEvent e) {//get the text of the click buttonString Text =E.getactioncommand (); /*working with numbers or operators*/ if(".". Equals (text) | | Character.isdigit (Text.charat (0))) {//Digit decimal pointhandlenumber (text); } Else if("+-*/=". IndexOf (text)! =-1) {//operatorhandleoperator (text); } } /*** Processing numbers and decimals * *@paramtext*/ Private voidhandlenumber (String text) {if(Isfirstdigit) {//First time input if(".". Equals (text)) { This. Result.settext ("0.")); } Else { This. Result.settext (text); } } Else if("0". Equals (text) && "0". Equals ( This. Result.gettext ())) {//Input 0Isfirstdigit =true; return; } Else if(".". Equals (text) && This. Result.gettext (). IndexOf (".") = =-1) {//Enter a decimal point This. Result.settext ( This. Result.gettext () + "."); } Else if(!".". Equals (text)) {//input is not a decimal point This. Result.settext ( This. Result.gettext () +text); } //Modify the first input tagIsfirstdigit =false; } /*** Processing operator * *@paramtext*/ Private voidhandleoperator (String text) {/*make a judgment of arithmetic operation*/ Switch(operator) { Case"+": Resultnum+ = Double.parsedouble ( This. Result.gettext ()); Break; Case"-": Resultnum-= Double.parsedouble ( This. Result.gettext ()); Break; Case"*": Resultnum*= double.parsedouble ( This. Result.gettext ()); Break; Case"/": Resultnum/= double.parsedouble ( This. Result.gettext ()); Break; Case"=": Resultnum= Double.parsedouble ( This. Result.gettext ()); Break; } //display the result of an operation in a text box This. Result.settext (string.valueof (resultnum)); //put a parameter operator in a member variable This. operator =text; //the next number is the first time you enterIsfirstdigit =true; } }
Java Basic Learning--swing graphical user interface programming