Package com. rgy. entity; import java. awt. borderLayout; import java. awt. font; import java. awt. gridLayout; import java. awt. event. actionEvent; import java. awt. event. actionListener; import javax. swing. borderFactory; import javax. swing. JButton; import javax. swing. JFrame; import javax. swing. JPanel; import javax. swing. JTextField; import javax. swing. windowConstants; @ SuppressWarnings ("serial") public class Calculator2 e Xtends JFrame {// declare the auxiliary variable private String str = "0"; // Save the private String show_str = ""; private double num_record = 0; // record the current data value private int operator = 0; // record the current operator type private int count_point = 0; // record the number of decimal places, avoid entering the decimal point repeatedly // declare and initialize each component private JTextField text_show = new JTextField ("0"); private JTextField text_show1 = new JTextField (); private JPanel panel_up = new JPanel (); private JPanel panel_CEandC = new JP Anel (); private JPanel panel_keys = new JPanel (); private JButton button_CE = new JButton ("CE"); private JButton button_C = new JButton ("C "); private JButton button_num0 = new JButton ("0"); private JButton button_num1 = new JButton ("1"); private JButton button_num2 = new JButton ("2 "); private JButton button_num3 = new JButton ("3"); private JButton button_num4 = new JButton ("4"); private JButton button_num5 = New JButton ("5"); private JButton button_num6 = new JButton ("6"); private JButton button_num7 = new JButton ("7 "); private JButton button_num8 = new JButton ("8"); private JButton button_num9 = new JButton ("9"); private JButton button_division = new JButton ("/"); private JButton button_multiplication = new JButton ("*"); private JButton button_cut = new JButton ("-"); private JButton button_add = new JButt On ("+"); private JButton button_equal = new JButton ("="); private JButton button_point = new JButton (". "); public Calculator2 (String s) {super (s); this. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); this. setBounds (500,250,270,400); this. setVisible (true); // set the display box text_show.setHorizontalAlignment (JTextField. RIGHT); text_show.setFont (new Font ("Default", 0, 30); text_show.setEditable (false); // The te cannot be edited. Xt_show1.setHorizontalAlignment (JTextField. RIGHT); text_show1.setFont (new Font ("Default", 0, 16); text_show1.setEditable (false); // set the layout and border panel_up.setLayout (new BorderLayout ()); button_CE.setBorder (BorderFactory. createRaisedBevelBorder (); button_C.setBorder (BorderFactory. createRaisedBevelBorder (); button_CE.setFont (new Font ("Default", 1, 20); button_C.setFont (new Font ("Default", 1, 20); panel_up.set Border (BorderFactory. createEtchedBorder (); panel_CEandC.setLayout (new GridLayout (1, 2, 7, 7); panel_keys.setLayout (new GridLayout (4, 4, 7, 7 )); // panel nested panel_up.add (text_show1, BorderLayout. NORTH); panel_up.add (text_show, BorderLayout. CENTER); panel_up.add (panel_CEandC, BorderLayout. SOUTH); this. add (panel_up, BorderLayout. NORTH); this. add (panel_keys, BorderLayout. CENTER); // Add the button panel_CEandC.add (Ton_CE); Round (button_C); Round (button_num7); Round (button_num8); Round (button_num9); Round (button_division); Round (button_num4); Round (button_num5 ); values (button_num6); panel_keys.add (button_multiplication); values (button_num1); values (button_num2); values (button_num3); values (button_cut); panel_keys.add (B Utton_num0); panel_keys.add (button_point); panel_keys.add (button_equal); panel_keys.add (button_add); // Add an event (anonymous internal class) listener (new ActionListener () {public void actionreceivmed (ActionEvent e) {if (operator! = 5) {if (text_show.getText (). equals ("0") {text_show.setText ("");} str = text_show.getText () + "0"; text_show.setText (str );} else {// before this, follow the equal sign setClear (); str = text_show.getText () + "0"; text_show.setText (str) ;}}); button_num1.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {if (operator! = 5) {if (text_show.getText (). equals ("0") {text_show.setText ("");} str = text_show.getText () + "1"; text_show.setText (str);} else {setClear (); str = text_show.getText () + "1"; text_show.setText (str) ;}}); button_num2.addActionListener (new ActionListener () {public void action=med (ActionEvent e) {if (operator! = 5) {if (text_show.getText (). equals ("0") {text_show.setText ("");} str = text_show.getText () + "2"; text_show.setText (str);} else {setClear (); str = text_show.getText () + "2"; text_show.setText (str) ;}}); button_num3.addActionListener (new ActionListener () {public void action=med (ActionEvent e) {if (operator! = 5) {if (text_show.getText (). equals ("0") {text_show.setText ("");} str = text_show.getText () + "3"; text_show.setText (str);} else {setClear (); str = text_show.getText () + "3"; text_show.setText (str) ;}}); button_num4.addActionListener (new ActionListener () {public void action=med (ActionEvent e) {if (operator! = 5) {if (text_show.getText (). equals ("0") {text_show.setText ("");} str = text_show.getText () + "4"; text_show.setText (str);} else {setClear (); str = text_show.getText () + "4"; text_show.setText (str) ;}}); button_num5.addActionListener (new ActionListener () {public void action=med (ActionEvent e) {if (operator! = 5) {if (text_show.getText (). equals ("0") {text_show.setText ("");} str = text_show.getText () + "5"; text_show.setText (str);} else {setClear (); str = text_show.getText () + "5"; text_show.setText (str) ;}}); button_num6.addActionListener (new ActionListener () {public void action=med (ActionEvent e) {if (operator! = 5) {if (text_show.getText (). equals ("0") {text_show.setText ("");} str = text_show.getText () + "6"; text_show.setText (str);} else {setClear (); str = text_show.getText () + "6"; text_show.setText (str) ;}}); button_num7.addActionListener (new ActionListener () {public void action=med (ActionEvent e) {if (operator! = 5) {if (text_show.getText (). equals ("0") {text_show.setText ("");} str = text_show.getText () + "7"; text_show.setText (str);} else {setClear (); str = text_show.getText () + "7"; text_show.setText (str) ;}}); button_num8.addActionListener (new ActionListener () {public void action=med (ActionEvent e) {if (operator! = 5) {if (text_show.getText (). equals ("0") {text_show.setText ("");} str = text_show.getText () + "8"; text_show.setText (str);} else {setClear (); str = text_show.getText () + "8"; text_show.setText (str) ;}}); button_num9.addActionListener (new ActionListener () {public void action=med (ActionEvent e) {if (operator! = 5) {if (text_show.getText (). equals ("0") {text_show.setText ("");} str = text_show.getText () + "9"; text_show.setText (str);} else {setClear (); str = text_show.getText () + "9"; text_show.setText (str) ;}}); button_point.addActionListener (new ActionListener () {public void action=med (ActionEvent e) {if (count_point = 0) {str = text_show.getText () + ". "; text_show.setText (str); count_point ++;} else {// if the number of decimal points exceeds one Return }}); button_C.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {// clear all, including data and the setClear () operator (); text_show.setText ("0") ;}}); button_CE.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {if (operator = 5) {// for data with an equal sign, all num_record = 0; str = ""; text_show.setText ("0");} is cleared ");} else {// do not decrease the number of digits in order from low to high if (str. length ()! = 0) {// if the data length in the displayed box is not 0str = text_show.getText (); str = str. substring (0, str. length ()-1); if (str. length ()> 1) {text_show.setText (str);} else {str = ""; text_show.setText ("0") ;}}}); button_add.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {if (str. equals ("") {} else {switch (operator) {case 0: num_record = Double. parseDouble (str); break; case 1: num_record = num_record + Double. parseDouble (str); break; case 2: num_record = num_record-Double. parseDouble (str); break; case 3: num_record = num_record * Double. parseDouble (str); break; case 4: num_record = num_record/Double. parseDouble (str); break; case 5: break;} show_str = show_str + text_show.getText () + "+"; text_show1.setText (show_str); str = ""; text_show.setText (""); operator = 1 ;}}); button_cut.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {if (text_show.getText (). equals ("0") {if (operator = 0) {// function str = "-"; text_show.setText (str) ;}else {return ;}} else {if (str. equals ("") {} else {switch (operator) {case 0: num_record = Double. parseDouble (str); break; case 1: num_record = num_record + Double. parseDouble (str); break; case 2: num_record = num_record-Double. parseDouble (str); break; case 3: num_record = num_record * Double. parseDouble (str); break; case 4: num_record = num_record/Double. parseDouble (str); break; case 5: break;} show_str = show_str + text_show.getText () + "-"; text_show1.setText (show_str); str = ""; text_show.setText (""); operator = 2 ;}}}); button_multiplication.addActionListener (new ActionListener () {public void action=med (ActionEvent e) {if (str. equals ("") {} else {switch (operator) {case 0: num_record = Double. parseDouble (str); break; case 1: num_record = num_record + Double. parseDouble (str); break; case 2: num_record = num_record-Double. parseDouble (str); break; case 3: num_record = num_record * Double. parseDouble (str); break; case 4: num_record = num_record/Double. parseDouble (str); break; case 5: break;} show_str = show_str + text_show.getText () + "*"; text_show1.setText (show_str); str = ""; text_show.setText (""); operator = 3 ;}}); button_division.addActionListener (new ActionListener () {public void action=med (ActionEvent e) {if (str. equals ("") {} else {switch (operator) {case 0: num_record = Double. parseDouble (str); break; case 1: num_record = num_record + Double. parseDouble (str); break; case 2: num_record = num_record-Double. parseDouble (str); break; case 3: num_record = num_record * Double. parseDouble (str); break; case 4: num_record = num_record/Double. parseDouble (str); break; case 5: break; }} show_str = show_str + text_show.getText () + "/"; text_show1.setText (show_str); str = ""; text_show.setText (""); operator = 4 ;}}); button_equal.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {if (str. equals ("") {} else {switch (operator) {case 0: num_record = Double. parseDouble (str); break; case 1: num_record = num_record + Double. parseDouble (str); break; case 2: num_record = num_record-Double. parseDouble (str); break; case 3: num_record = num_record * Double. parseDouble (str); break; case 4: num_record = num_record/Double. parseDouble (str); break; case 5: break; }} show_str = ""; // The output shows text_show1.setText (""); if (num_record-(int) num_record = 0) {text_show.setText ("" + (int) num_record);} else {if ("" + num_record ). length ()> 15) {text_show.setText ("" + num_record ). substring ();} else {text_show.setText ("" + num_record) ;}} operator = 5 ;});} public void setClear () {// clear num_record = 0; operator = 0; str = ""; text_show.setText (""); show_str = ""; text_show1.setText ("");} public static void main (String args []) {new Calculator2 ("Calculator v1.02 ");}}