Home > Back-end >  Java to make simple calculator
Java to make simple calculator

Time:10-30

UserFrm. Java
import java.awt.*;
Import the Java. The awt. Event. *;

Public class UserFrm extends Frame implements ActionListener {
Private MenuBar JMB=new MenuBar ();
Private MenuItem item=new MenuItem (" take out ");
Public static Font Font=new Font (" tahoma ", 1, 16); Public UserFrm (String title) throws HeadlessException {super (title);//code [1]//set the size and location Frame and specific value of (100, 100, 250, 200) setVisible (true); The Menu Menu=new Menu (" documents "); Menu. The add (item); Item. The setFont (font); The Panel panelNorth=new Panel ();//code [2]//add panelNorth to form the north to the add (new Caculator (), BorderLayout. CENTER); Item. AddActionListener (this); JMB. Add (menu); JMB. SetFont (font); SetMenuBar (JMB);//code [3]//design to complete the form in an anonymous class way close surveillance and implementation method of the validate ();
} public void actionPerformed (an ActionEvent e) {Object o=um participant etSource (); If (o==item) System. The exit (1);
} public static void main (String [] args) {new UserFrm (" user interface ");
}
}

The class Caculator extends the Panel implements ActionListener, KeyListener {
Private TextField tf=new TextField (" ");
Private float x=0;
Private float y=0;
Private int code=0;
Private Boolean enable;
Private Boolean first;
Private String STR=""; Public Caculator () {setLayout (new BorderLayout ()); The enable=true; The first=true; Add (tf, BorderLayout. NORTH); The Panel Panel=new Panel ();//code [4]//set the panel to GridLayout layout Button BTN=null;//code [5]/BTN/created and the title is set to 1; The BTN add to the panel; Set the button font format for UserFrm. The font. Set the input validation to monitor; Set the keyboard to monitor/BTN/code [6]//created and set the title for the 2; The BTN add to the panel; Set the button font format for UserFrm. The font. Set the input validation to monitor; Set the keyboard to monitor/BTN/code [7]//created and set title to 3; The BTN add to the panel; Set the button font format for UserFrm. The font. Set the input validation to monitor; Set the keyboard to monitor//[8] code/BTN/created and set title to +; The BTN add to the panel; Set the button font format for UserFrm. The font. Set the input validation to monitor; Set the keyboard to monitor/BTN/code [9]//created and set title is 4; The BTN add to the panel; Set the button font format for UserFrm. The font. Set the input validation to monitor; Set the keyboard to monitor//[10] code/BTN/created and set the title for 5; The BTN add to the panel; Set the button font format for UserFrm. The font. Set the input validation to monitor; Set the keyboard to monitor/BTN/code [11]//created and set title is 6; The BTN add to the panel; Set the button font format for UserFrm. The font. Set the input validation to monitor; Set the keyboard to monitor//[12] code/BTN/created and set title -; The BTN add to the panel; Set the button font format for UserFrm. The font. Set the input validation to monitor; Set the keyboard to monitor/BTN/code [13]//created and set title at 7; The BTN add to the panel; Set the button font format for UserFrm. The font. Set the input validation to monitor; Set the keyboard to monitor//[14] code/BTN/created and set the title of 8; The BTN add to the panel; Set the button font format for UserFrm. The font. Set the input validation to monitor; Set the keyboard to monitor///[15] code/BTN/created and set the title for 9; The BTN add to the panel; Set the button font format for UserFrm. The font. Set the input validation to monitor; Set the keyboard to monitor/BTN/code [16]//created and set title is *; The BTN add to the panel; Set the button font format for UserFrm. The font. Set the input validation to monitor; Set the keyboard to monitor//[17] code BTN//created and put the title to 0; The BTN add to the panel; Set the button font format for UserFrm. The font. Set the input validation to monitor; Setting the keyboard monitor//[18] code/BTN/created and set the title for.; The BTN add to the panel; Set the button font format for UserFrm. The font. Set the input validation to monitor; Set the keyboard to monitor//[19] code/BTN/created and set the title to/; The BTN add to the panel; Set the button font format for UserFrm. The font. Set the input validation to monitor; Set the keyboard monitor BTN=new Button ("="); A panel. The add (BTN); BTN. SetFont (UserFrm. The font); BTN. AddActionListener (this); BTN. AddKeyListener (this);//code [20]//add a panel to the central region of Caculator
{if} public void actionPerformed (an ActionEvent e) (um participant etActionCommand ()=="+") {//[21] code//convert the figures in the text box to float, code assigned to x=0; This. Tf. SetText (" "); } the if (um participant etActionCommand ()=="-") {//[21] code//convert the figures in the text box to float, code assigned to x=1; This. Tf. SetText (" "); } the if (um participant etActionCommand ()=='*') {//[21] code//convert the figures in the text box to float, code assigned to x=2; This. Tf. SetText (" "); } the if (um participant etActionCommand ()=="/") {x=Float. The parseFloat (tf) getText ()); Code=3; This. Tf. SetText (" "); } the if (um participant etActionCommand ()!="+" & amp; & Um participant etActionCommand ()!="-" & amp; & Um participant etActionCommand ()!="*" & amp; & Um participant etActionCommand ()!="/" & amp; & Um participant etActionCommand ()!={"=") if (enable) {if (first) {tf. The setText (um participant etActionCommand ()); The first=false; } else {tf. SetText (tf) getText () + um participant etActionCommand ()); }} else {tf. SetText (um participant etActionCommand ()); The enable=true; }} the if (um participant etActionCommand ()=="=") {switch (code) {case 0://code [22]//the number of completed two additive operation tf setText (Float. ToString (y)); The enable=false; break; Case 1://[23] code//finish two subtract tf setText (Float. ToString (y)); The enable=false; break; Case 2://code [24]//the number of completed two multiplication tf. The setText (Float. ToString (y)); The enable=false; break; Case 3://code [25]//the number of completed two division tf setText (Float. ToString (y)); The enable=false; break; }}
{if} public void keyPressed KeyEvent (e) (um participant etKeyChar ()=='+') {x=Float. The parseFloat (tf) getText ()); Code=0; This. Tf. SetText (" "); } the if (um participant etKeyChar ()=='-') {x=Float. The parseFloat (tf) getText ()); Code=1; This. Tf. SetText (" "); } the if (um participant etKeyChar ()=='*') {x=Float. The parseFloat (tf) getText ()); Code=2; This. Tf. SetText (" "); } the if (um participant etKeyChar ()=='/') {x=Float. The parseFloat (tf) getText ()); Code=3; This. Tf. SetText (" "); } the if (26 [code]) {//whether use keyboard input Numbers 0-9 and., one of them, the if (enable) {//code [27]//in the text box input and display a number, when the input Numbers again, ten times to enlarge the original data and the second input data again,//tip: first to determine whether the digital input data for the first time; Use string combined to ten times magnification,} else {STR=Character. ToString (um participant etKeyChar ()); Tf. SetText (STR); The enable=true; }} the if (28 [code]) {//judging by keyboard input is the key to enter the confirmation key, the switch (code) {case 0: y=x + Float. The parseFloat (enclosing tf. The getText ());//the number of completed two additive operation tf. The setText (Float. ToString (y)); The enable=false; break; Case 1: y=x - Float. ParseFloat (enclosing tf. The getText ());//the number of completed two subtract tf. The setText (Float. ToString (y)); The enable=false; break; Case 2: y=x * Float in parseFloat (enclosing tf. The getText ());//the number of completed two multiplication tf. The setText (Float. ToString (y)); The enable=false; break; Case 3: y=x/Float parseFloat (enclosing tf. The getText ()); nullnullnullnullnullnullnullnullnullnullnull
  • Related