This calculator is an exercise for Swing, and the logic is relatively simple.
It can only perform a single operation (not a polynomial operation), considering that the divisor cannot be 0
Import java. awt. *; <br/> import java. awt. event. *; <br/> import javax. swing. *; <br/> public class Calculator extends JFrame <br/> implements ActionListener {<br/> JTextField jtf = new JTextField (); <br/> JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0; <br/> JButton badd, bsub, bmul, bdiv, bequal, bdot; <br/> int select = 0; <br/> JPanel jp1 = new JPanel (); <br/> // 0 ~ 9. Number and addition, subtraction, multiplication, division, and point symbol <br/> double temp = 0.0; <br/> double num = 0.0; <br/> public Calculator () {<br/> // generate two areas. The above area is a text field, and the following is the calculator button <br/> this. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); <br/> Container c = this. getContentPane (); <br/> jtf. setHorizontalAlignment (JTextField. RIGHT); <br/> c. add (jtf, BorderLayout. NORTH); <br/> c. add (jp1, BorderLayout. CENTER); </p> <p> jp1.setLayout (new GridLayout (4); </p> <p> b1 = new JButton ("1 "); <br/> b1.addActionListener (this); <br/> jp1.add (b1); </p> <p> b2 = new JButton ("2 "); <br/> b2.addActionListener (this); <br/> jp1.add (b2); </p> <p> b3 = new JButton ("3 "); <br/> b3.addActionListener (this); <br/> jp1.add (b3); </p> <p> badd = new JButton ("+ "); <br/> badd. addActionListener (this); <br/> jp1.add (badd); </p> <p> b4 = new JButton ("4"); <br/> b4.addActionListener (this ); <br/> jp1.add (b4); </p> <p> b5 = new JButton ("5"); <br/> b5.addActionListener (this ); <br/> jp1.add (b5); </p> <p> b6 = new JButton ("6"); <br/> b6.addActionListener (this ); <br/> jp1.add (b6); </p> <p> bsub = new JButton ("-"); <br/> bsub. addActionListener (this); <br/> jp1.add (bsub); </p> <p> b7 = new JButton ("7"); <br/> b7.addActionListener (this ); <br/> jp1.add (b7); </p> <p> b8 = new JButton ("8"); <br/> b8.addActionListener (this ); <br/> jp1.add (b8); </p> <p> b9 = new JButton ("9"); <br/> b9.addActionListener (this ); <br/> jp1.add (b9); </p> <p> bmul = new JButton ("*"); <br/> bmul. addActionListener (this); <br/> jp1.add (bmul); </p> <p> b0 = new JButton ("0"); <br/> b0.addActionListener (this ); <br/> jp1.add (b0); </p> <p> bdot = new JButton (". "); <br/> bdot. addActionListener (this); <br/> jp1.add (bdot); </p> <p> bequal = new JButton ("="); <br/> bequal. addActionListener (this); <br/> jp1.add (bequal); </p> <p> bdiv = new JButton ("//"); <br/> bdiv. addActionListener (this); <br/> jp1.add (bdiv); <br/>}< br/> public void actionreceivmed (ActionEvent e) {<br/> // jtf. setText (jtf. getText () + e. getActionCommand (); <br/> if (e. getSource () = b1) {<br/> jtf. setText (jtf. getText () + e. getActionCommand (); <br/>}< br/> if (e. getSource () = b2) {<br/> jtf. setText (jtf. getText () + e. getActionCommand (); <br/>}< br/> if (e. getSource () = b3) {<br/> jtf. setText (jtf. getText () + e. getActionCommand (); <br/>}< br/> if (e. getSource () = b4) {<br/> jtf. setText (jtf. getText () + e. getActionCommand (); <br/>}< br/> if (e. getSource () = b5) {<br/> jtf. setText (jtf. getText () + e. getActionCommand (); <br/>}< br/> if (e. getSource () = b6) {<br/> jtf. setText (jtf. getText () + e. getActionCommand (); <br/>}< br/> if (e. getSource () = b7) {<br/> jtf. setText (jtf. getText () + e. getActionCommand (); <br/>}< br/> if (e. getSource () = b8) {<br/> jtf. setText (jtf. getText () + e. getActionCommand (); <br/>}< br/> if (e. getSource () = b9) {<br/> jtf. setText (jtf. getText () + e. getActionCommand (); <br/>}< br/> if (e. getSource () = b0) {<br/> jtf. setText (jtf. getText () + e. getActionCommand (); <br/>}< br/> if (e. getSource () = badd) {<br/> temp = Double. parseDouble (jtf. getText (); <br/> jtf. setText (""); <br/> select = 1; <br/>}< br/> if (e. getSource () = bsub) {<br/> temp = Double. parseDouble (jtf. getText (); <br/> jtf. setText (""); <br/> select = 2; <br/>}< br/> if (e. getSource () = bmul) {<br/> temp = Double. parseDouble (jtf. getText (); <br/> jtf. setText (""); <br/> select = 3; <br/>}< br/> if (e. getSource () = bdiv) {<br/> temp = Double. parseDouble (jtf. getText (); <br/> jtf. setText (""); <br/> select = 4; <br/>}< br/> if (e. getSource () = bequal) {<br/> if (select = 1) {<br/> num = Double. parseDouble (jtf. getText (); <br/> jtf. setText (num = temp + num) + ""); <br/>}< br/> else if (select = 2) {<br/> num = Double. parseDouble (jtf. getText (); <br/> jtf. setText (num = temp-num) + ""); <br/>}< br/> else if (select = 3) {<br/> num = Double. parseDouble (jtf. getText (); <br/> jtf. setText (num = temp * num) + ""); <br/>}< br/> else if (select = 4) {<br/> num = Double. parseDouble (jtf. getText (); <br/> if (num = 0) {<br/> JOptionPane. showMessageDialog (jp1, "the divisor cannot be 0"); <br/>}< br/> jtf. setText (num = temp/num) + ""); <br/>}< br/> public static void main (String [] args) {<br/> Calculator mainFrame = new Calculator (); <br/> mainFrame. setSize (400,400); <br/> mainFrame. setTitle ("calculator by jk"); <br/> mainFrame. setVisible (true); <br/>}< br/>}