Import java.awt. *; import java.awt.event.ActionEvent; import java.awt.event.Actionlistener; import java.awt.event.windowadapter; import java.awt.event.windowEvent;
Import javax.swing.jtextfield; import java.awt.color;
Public class createframe {frame f; jtextfield text, text2; Button B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12, B13, B14, B15, B16, B17, B18, B19 The three numbers of B20, B21, B22, B23, B24, B25, B26, B27; Double OP1, OP2, OP2X, OP3; // are operational calculations, OP3 is the calculation result char OP = 0, OPX = 0, OPP = 0; // Computing Operators Boolean B = false; // I pressed the 0 button, next time it should be pressed. Boolean c = false; // Press the operator, next time When this clear screen shows boolean firstdeng = true; // for the first time =
Double MJIA = 0;
public static void main (String [] args) {createFrame cf = new createFrame (); cf.createWindow ();} public void createWindow () {f = new Frame ( "calculator"); f.setBackground (Color.LIGHT_GRAY) ; // increase the listener this.shutwindow (f) that closes the event;
Text = new jtextfield (); text.sethorizontalalignment (jtextfield.right); text.setsize (250, 22); text.setLocation (5, 25); text.seteditable (false); Text.setBackground (color.White); Text.Settext ("0."); text2 = new jtextfield (); text2.sethorizontalalignment (jtextfield.center); Text2.setsize (37, 22); Text2.SetLocation (5,50); Text2.setedITable (false) Text2.SetText (""); text2.setBackground; b1 = new button ("backspace"); b1.setsize (70, 22); b1.setlocation (52, 50); b2 = new button ("CE"); b2.setsize (60, 22); b2.setlocation (128, 50); b3 = new button); b3.setsize (60, 22); b3.setlocation (194, 50 ); B4 = new button ("mc"); b4.setsize (37, 22); b4.setlocation (5,75); b5 = new button ("7"); b5.setsize (37, 22); b5 .SETLOCATION (50, 75); b6 = new Button ("8"); b6.setsize (37,22); b6.setlocation (93,75); b7 = new button ("9"); b7.setsize (37, 22); b7.setlocation (136, 75); b8 = new button ("/"); b8.setsize (37, 22); b8.setlocation (179, 75); b9 = new button ("sqrt"); b9.setsize (37, 22); B9.SetLocation (218,75); b10 = new button ("mr"); b10.setsize (37, 22); b10.setlocation (5,100); b11 = new button ("4"); b11.setsize (37 , 22); B11.SetLocation (50, 100); B12 = New Button ("5"); b12.setsize (37, 22);
B12.SetLocation (93, 100); B13 = New Button ("6"); b13.setsize (37, 22); b13.setLocation (136, 100); b14 = new button ("*"); b14.setsize (37, 22 ); B14.SetLocation (179, 100); b15 = new button ("%"); b15.setsize (37, 22); b15.SetLocation (218,100); b16 = New Button ("MS"); b16.setsize (37 , 22); B16.SETLOCATION (5,125); b17 = new button ("1"); b17.setsize (37, 22); b17.setlocation (50, 125); b18 = new button ("2"); b18.setsize (37, 22); B18.SETLOCATION (93, 125); b19 = New Button ("3"); b19.setsize (37, 22); b19.setlocation (136, 125); b20 = new button ("-"); b20 .SETSIZE (37, 22); B20.SETLOCATION (179, 125); B21 = New Button ("1 / x"); b21.setsize (37, 22); b21.setlocation (218, 125); b22 = new button ("m "); B22.setsize (37, 22); b22.setlocation (5,150); b23 = new button (" 0 "); b23.setsize (37, 22); B23.SETLOCATION (50, 150); B24 = New Button (" /-"); b24.setsize (37, 22); b24.setlocation (93, 150); b25 = new button ("."); b25.setsize (37 , 22); B26.SetLocation (" "); b26.setlocation (379, 150); b27 = new button ("="); b27 = new button ("="); b27.setsize (37, 22); B27.SETLOCATION (218, 150); f.setlayout (null);
F.Add (text); f.add (text2); b1.addActionListener (New ButtonHandler ()); b3.addactionListener (New ButtonHandler ()); b4.addactionListener (New ButtonHandler ()); b5.addActionListener (new ButtonHandler ()); b6.addActionListener (new ButtonHandler ()); b7.addActionListener (new ButtonHandler ()); b8.addActionListener (new ButtonHandler ()); b9.addActionListener (new ButtonHandler ()); b10.addActionListener (new ButtonHandler ()); b11.addActionListener (new ButtonHandler ()); b12.addActionListener (new ButtonHandler ()); b13.addActionListener (new ButtonHandler ()); b14.addActionListener (new ButtonHandler ()); b15.addActionListener (new ButtonHandler ()); b16.addActionListener (new ButtonHandler ()); b17.addActionListener (new ButtonHandler ()); b18.addActionListener (new ButtonHandler ()); b19.addActionListener (new ButtonHandler ()) ; B20.addActionListener (new ButtonHandler ()); b21.addActionListener (new ButtonHandler ()); b22.addActionListener (new ButtonHandler ()); b23.addActionListener (new ButtonHandler ()); b24.addActionListener (new ButtonHandler ()) B25.AddActionListener (New ButtonHandler ()); New ButtonHandler ()); b27.addactionListener (New ButtonHandler ());
F.Add (b1); F.Add (b2); F.Add (b4); f.Add (b5); f.Add (b6); f.Add (b7); f .ADD (B8); F.Add (b9); F.Add (b11); F.Add (b12); f.Add (b13); f.Add (b14); f. Add (b15); F.Add (b16); F.Add (b17); f.Add (b19); f.Add (b20); f.Add (b21); f.Add (b22); F.Add (b24); F.Add (b25); f.Add (b26); f.Add (b27); f.setResizable (false); f.setsize 260, 180); // Window Display THISCENTERSHOW (260, 180, F); F.SetVisible (TRUE);} public void centershow (int X, int y, frame f) {// window Creation display Toolkit TK = Toolkit.getDefaultToolkit (); Dimension D = Tk.getscreensize (); int ScreenHeight = D.height; int screenwidth = d.width; f.setLocation (ScreenWidth-x) / 2, (ScreenHeight-Y) / 2);} public void shutwindow (frame f) {// add a listener for the close event f.addwindowlistener (new windowadapter () {public void windowclosing (windowEvent evt) {// exit program system.exit (0);} });} Public double getCurrentNum () // Get the current text box Digital {RETURN 1;} // Four operations public Double Calculatenow (Double OP1, Char OP, Double OP2) {Double OP1B = 0.0; Switch (OP) { Case ' ': OP1B = OP1 OP2; BREAK; CASE '-': OP1B = OP1-OP2; Break; Case '*': OP1B =
Op1 * OP2; Break; Case '/': IF (OP2 == 0.0) OP1B = OP1; ELSE OP1B = OP1 / OP2; Break; Case '%': IF (OP2 == 0.0) OP1B = OP1; ELSE OP1B = op1% op2; break; case 0: op1b = op2; break;} return op1b;} class ButtonHandler implements ActionListener {public void actionPerformed (ActionEvent e) {System.out.println ( "Button's lable is:" e.getActionCommand ( ) "||" createframe.THIS.OP1 CREATEFRAME.THIS.OP CREATEFRAME.THIS.OP CREATEFRAME.THIS.OP2 "=" CreateFrame.THIS.OP3); string command = E.GetActionCommand (); string currenttext = text. GetText (); if ("CurrentText.equals (" function input is invalid. ") || CurrentText.Equals (" The divisor cannot be zero. ")) {currenttext =" 0. "; text.setText (CURRETTEXT); IF (COMMAND == "Backspace") {if (currenttext.length ()> 1) CurrentText = CurrentText.Substring (0, currenttext.length () - 1); else f (currenttext.length () <= 1) { CurrentText = "0.";} text.setText (CURRENTTEXT);} if (command == "CE") {text.setText ("0."); op2 = 0.0; // Return;} if (command == "C"
) {Text.setText ("0."); OP3 = 0.0; OP1 = 0.0; OP2 = 0.0; OP2X = 0.0; OP = 0; OPX = 0; OPP = 0; firseg = true; b = false; c = False;}}} (text.getText (). Equals ("0.")) {text.setText ("0."); b = true;} else text.setText CurrentText "0");} if (command == "1") {if (c) {c = false; text.setText ("1"); return;} if ((text.getText (). Equals. 0. ") || text.gettext (). Equals (" 0 ")) &! B) text.settext (" 1 "); else text.settext (CurrentText " 1 ");} if (command ==" 2 ") {if (c) {text .SETTEXT ("2"); c = false; return;} IF ((text.getText (). Equals ("0.") || text.getText (). Equals ("0")) &! b) Text.setText ("2"); Else Text.Settext (CurrentText "2"); // Return;} if (Command == "3") {if (c) {c = false; text.setText ("3 "); RETURN;
} IF ((text.getText (). Equals ("0.") || text.getText (). Equals ("0")) &! B) text.setText ("3"); else text.setText ( CurrentText "3"); // Return;} if (Command == "4") {if (c) {c = false; text.setText ("4"); return;} if ((text.getText () . Equals ("0.") || text.getText (). Equals ("0")) &! b) Text.Settext ("4"); Else Text.Settext (CurrentText "4"); // Return } IF (command == "5") {if (c) {c = false; text.setText ("5"); return;} IF ((text.getText (). Equals ("0.") | | text.getText (). Equals ("0")) &! b) text.setText ("5"); Else Text.Settext (CurrentText "5"); // Return;} if (command == "6 ") {IF (c) {c = false; text.setText ("6"); return;} if ((text.getText (). Equals ("0.") || text.getText (). Equals ("0) ")) &! b) Text.Settext (" 6 "); Else Text.Settext (CurrentText " 6 "); // Return;} IF (command ==" 7 ") {i (c) {c = false Text.setText ("
7 "); Return;} IF ((text.getText (). Equals (" 0. ") || text.getText (). Equals (" 0 ")) &! B) Text.SetText (" 7 ") Else text.Settext (CurrentText "7"); // Return;} if (command == "8") {if (c) {c = false; text.setText ("8"); return;} IF (Text.getText (). Equals ("0.") || text.getText (). Equals ("0")) &! b) Text.Settext ("8"); else text.settext (CurrentText "8 "); // Return;}} (command ==" 9 ") {if (c) {c = false; text.setText (" 9 "); return;} if ((text.getText (). Equals. "0.") || text.getText (). Equals ("0")) &! B) Text.Settext ("9"); Else Text.Settext (CurrentText "9");} IF (command == "/") {op2 = double.Valueof (text.getText ()). DoubleValue (); if (OPP! = '=') {OP1 = CalculatenOw (OP1, OP, OP2); OPP = '/';} OP = '/'; opx = '/'; text.settext (String.Valueof (OP1)); c = true; // b = false; firstdeng = true;
} If (command == "*") {op2 = double.Valueof (text.getText ()). DoubleValue (); if (OPP! = '=') {OP1 = CalculatenOw (OP1, OP, OP2); OPP = '*';} OP = '*'; OPX = '*'; text.Settext (String.Valueof (OP1)); c = true; firstdeng = true;} if (command == ") {OP2 = Double.Valueof (Text.getText ()). DoubleValue (); if (OPP! = '=') {OP1 = Calculatenow (OP1, OP, OP2); OPP = '-';} OP = '-'; OPX = '-'; text.Settext (String.Valueof (OP1)); c = true; firstdeng = true;} if (command == " ") {op2 = double.Valueof (Text.getText ()). DoubleValue (); if (OPP! = '=') {OP1 = CalculatenOw (OP1, OP, OP2); OPP = ' '; } OP = ' '; OPX = ' '; text.Settext (String.Valueof (OP1)); c = true; firstdeng = true;} if (Command == ") {if (Text.getText (Text.getText) ) .indexof ('.')> 1) Return; else if (). Indexof ('.') == - 1) {text.setText (CurrentText "); b = True;
} Else if (Text.getText (). Indexof ('.') == 1) b = true;} if (command == " /-") {if (Double. Valueof ()). DoubleValue ()! = 0) {double d = -double.valueof (text.getText ()). doubleValue (); text.Settext (String.Valueof (D)); op2 = D; op2x = D;}}} (Command == "%") {op2 = double.Valueof (text.getText ()). DoubleValue (); if (op1! = 0 & op2! = 0) {Double Temp = OP1 * OP2; TEMP = TEMP / 100; text.Settext (String.Valueof (TEMP));}} f (command == "sqrt") {Double IN = double.Valueof (text.getText ()). DoubleValue (); if (in <0) Text.setText ("Function input is invalid."); else {double sd = math.sqrt (in); text.set TEXT (STRING.VALUEOF (SD)); OP2 = SD ;OP2X = SD;}
} IF (command == "1 / x") {double = double.valueof (text.getText ()). DoubleValue (); if (in == 0) text.setText ("The division cannot be zero.") Else {Double SD = 1 / in; text.Valueof (SD)); OP2 = SD; OP2X = SD;}} = {op2 == "=") {op2 = double.valueof (text. GetText ()). DoubleValue (); system.out.println ("=========== OP2); if (firstdeng) {OP2X = op2; firstdeng = false;} OP1 = Calculating (OP1 , OP, OP2X); OPP = '='; text.Settext (String.Valueof (OP1));} if (command == "mc") {mjia = 0; Text2.SetText (");} IF Command == "mr") {text.setText (String.Valueof (mjia));}} (command == "ms") { MJIA = double.Valueof (Text.getText ()). DoubleValue (); Text2.Settext ("m");} if (command == "m ") {mjia = mjia double.Valueof (Text.getText () .doublevalue ();}}}}