To write an application that simulates a calculator, using the Panel and grid layout, add a text box, 10 number buttons (0~9), 4 subtraction buttons, an equal sign button, a purge button, a square root button, a backspace button that asks the calculation formula and results to be displayed in a text box. The implementation effect is shown in the following illustration.
Java Simple Calculator Code:
Import javax.swing.*;
Import Javax.swing.JTextField;
Import java.awt.*;
Import java.awt.event.*;
Import java.lang.*;
Import Java.awt.Color; public class Ex5_2 extends JFrame implements ActionListener {private JPanel = new P1 ();//create panel private JPanel
el P2 = new JPanel (); Private JTextField T1; Text box 1 is used to display input information private JTextField T2; Text box 2 is used to display the result information private JLabel label; tag Information StringBuffer str; The string displayed by the display is double x,y; Both x and y are operands int z; Z indicates that the operator was clicked. 0 means "+", 1 means "-", 2 means "*", 3 means "/" private JButton b[] = new JBUTTON[12]; Create an array with 12 buttons private JButton b1,b2,b3,b4,b5,b6,b7,b8; Arithmetic function button public ex5_2 () {super ("simple Calculator"); Window name Container c = Getcontentpane ();
Create a content panel object t1 = new JTextField (30); T1.seteditable (FALSE);
Can only be displayed, cannot edit t2 = new JTextField (30); T2.seteditable (FALSE); Can only display, cannot edit label = new JLabel ("Welcome to the wizard version of the calculator ^_^ o~ efforts!"
"); Label.setforeground (Color.Blue);
Creates an empty string buffer str=new stringbuffer (); P2.add (label); Add tags to panel p2.add (T2); Add text box to panel p2.add (T1); Add a text box to the panel p2.setlayout (new GridLayout (4,1));
The panel layout is set to label 4 rows of 1 columns for (int i=0;i<10;i++)//buttons for the 0~9 number in the array and registers the listener {String s= "" +i;
b[i]= new JButton (s);
B[i].addactionlistener (this); //Instantiate each button b[10]= new JButton ("-/+");
b[11]= New JButton ("."); b1= New JButton ("/");
b2= New JButton ("Back"); b3= New JButton ("*");
b4= new JButton ("C"); b5= New JButton ("+");
b6= new JButton ("Sqrt"); b7= New JButton ("-");
b8= new JButton ("=");
Sets the button foreground color for (int i=0;i<12;i++) {b[i].setforeground (Color.Blue); } b1.setforeground (color.red);
B3.setforeground (color.red); B5.setforeground (color.red);
B7.setforeground (color.red); B2.setforeground (Color.Blue);
B4.setforeground (Color.Blue); B6.setforeground (color.red); B8.setForeground (Color.Blue); Add to Panel P1.add (B[7]); P1.add (B[8]); P1.add (b[9]); P1.add (B1);
P1.add (B2); P1.add (B[4]); P1.add (B[5]); P1.add (B[6]); P1.add (B3);
P1.add (B4); P1.add (b[1]); P1.add (b[2]); P1.add (B[3]); P1.add (B5);
P1.add (B6); P1.add (B[0]); P1.add (b[10]);
P1.add (b[11]);p 1.add (B7);p 1.add (B8);
P1.setlayout (New GridLayout (4,5,5,5)); Register Listener B[10].addactionlistener (this);
B[11].addactionlistener (this); B1.addactionlistener (this);
B2.addactionlistener (this); B3.addactionlistener (this);
B4.addactionlistener (this); B5.addactionlistener (this);
B6.addactionlistener (this); B7.addactionlistener (this);
B8.addactionlistener (this);
Add the Panel to the content panel C.add (p2);
C.add (p1); C.setlayout (New FlowLayout ()); Set to Sequential layout setdefaultcloseoperation (jframe.exit_on_close); Sets the window Close action setvisible (TRUE); Set to Visible setresizable (false); Prohibit the adjustment of frame size setSize (400,300);
Set Window Size}
The Main method implementation creates a window public static void main (string[] args) {ex5_2 f = new Ex5_2 ();}
button event handling public void actionperformed (ActionEvent e) {try {if (E.getsource () ==b4)//select "C" Clear 0 { T1.settext ("0"); Clear the text box 0 t1.sethorizontalalignment (jtextfield.right); Text aligned to right str.setlength (0); Empties the string buffer to prepare to receive the new input operand} else if (E.getsource () ==b[10])//click "+ +" to select whether the input count is positive or negative {X=double.parsedoub
Le (T1.gettext (). Trim ());//trim function is to remove the space T1.settext ("" + (x)) in the string;
T1.sethorizontalalignment (Jtextfield.right);
else if (E.getsource () ==b5)//Click the plus button to get X's value and Z's value and empty Y's value {x=double.parsedouble (T1.gettext (). Trim ());
Str.setlength (0);
y=0d;
z=0;
else if (E.getsource () ==b7)//Click the minus button to get X's value and Z's value and empty Y's value {x=double.parsedouble (T1.gettext (). Trim ());
Str.setlength (0);
y=0d;
Z=1;
else if (E.getsource () ==b3)//Click the multiplication button to get X's value and Z's value and empty Y's value { X=double.parsedouble (T1.gettext (). Trim ());
Str.setlength (0);
y=0d;
z=2;
else if (E.getsource () ==b1)/Click the Division button to get the value of X and Z and empty the value of y {x=double.parsedouble (T1.gettext (). Trim ());
Str.setlength (0);
y=0d;
z=3;
else if (E.getsource () ==b8)//Click the equals button to output the computed result {str.setlength (0); Switch (z) {case 0:t1.settext ("" + (X+y));
T1.sethorizontalalignment (Jtextfield.right); Case 1:t1.settext ("" + (x-y));
T1.sethorizontalalignment (Jtextfield.right); Case 2:t1.settext ("" + (X*y));
T1.sethorizontalalignment (Jtextfield.right); Case 3:t1.settext ("" + (x/y));
T1.sethorizontalalignment (Jtextfield.right); } else if (E.getsource () ==b[11])//click. button to enter a decimal {if (T1.gettext). Trim (). IndexOf ('. ')! =-1)//Determine if the string already contains the decimal point {} else//If there is no decimal point {if (T1.gettext (). Trim (). Equals ("0")//if the first time
Displayed as 0 { T1.settext (Str.append (E.getactioncommand ()). ToString ());
T1.sethorizontalalignment (Jtextfield.right); else if (T1.gettext (). Trim (). Equals ("")///If initially displayed as empty then do no action {} else {T1.s
Ettext (Str.append (E.getactioncommand ()). ToString ());
T1.sethorizontalalignment (Jtextfield.right);
}} y=0d;
else if (E.getsource () ==b6)//square root {x=double.parsedouble (T1.gettext (). Trim ());
if (x<0) {t1.settext ("number format exception");
T1.sethorizontalalignment (Jtextfield.right);
else {t1.settext ("" +math.sqrt (x));
T1.sethorizontalalignment (Jtextfield.right);
} str.setlength (0);
y=0d; else {if (E.getsource () ==b[0])//If you select "0" this number key {if (T1.gettext ()). Trim (). Equals ("0")) If the display of the display is zero operation {} else T1.settext (Str.append (E.getactioncommand) (). ToString ());
T1.sethorizontalalignment (Jtextfield.right);
Y=double.parsedouble (T1.gettext (). Trim ());
else if (E.getsource () ==b2)//Selected is the back key {if (!t1.gettext). Trim (). Equals ("0")//If the display is not 0 {if (Str.length ()!=1) {T1.settext (Str.delete (Str.length () -1,str.length ()). ToString ())
;//May throw string out of bounds exception t1.sethorizontalalignment (jtextfield.right);
else {T1.settext ("0"); t1.sethorizontalalignment (jtextfield.right);
Str.setlength (0);
} y=double.parsedouble (T1.gettext (). Trim ());
else {t1.settext (Str.append (E.getactioncommand ()). ToString ());
T1.sethorizontalalignment (Jtextfield.right);
Y=double.parsedouble (T1.gettext (). Trim ());
'} ' catch (NumberFormatException E1) {t1.settext ("number format exception"); T1.sethorizontalalignment (JTEXTFIELD.Right);
catch (Stringindexoutofboundsexception E1) {t1.settext ("string index out of Bounds");
T1.sethorizontalalignment (jtextfield.right);}
}
}
Run Effect chart:
A Java Simple calculator program design is so complete, hope this article can write calculator to have inspiration, this is only a simple calculator, everybody still can continue to play, perfect calculator function.