1
+ import java .awt .BorderLayout ;
2
+ import java .awt .GridBagConstraints ;
3
+ import java .awt .GridBagLayout ;
4
+ import java .awt .event .ActionEvent ;
5
+ import java .awt .event .ActionListener ;
6
+
7
+ import javax .swing .JButton ;
8
+ import javax .swing .JFrame ;
9
+ import javax .swing .JPanel ;
10
+ import javax .swing .JTextField ;
11
+
12
+ public class Calculator implements ActionListener {
13
+ private static JTextField inputBox ;
14
+
15
+ Calculator (){}
16
+ public static void main (String [] args ) {
17
+ createWindow ();
18
+ }
19
+
20
+ private static void createWindow () {
21
+ JFrame frame = new JFrame ("Calculator" );
22
+ frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
23
+
24
+ createUI (frame );
25
+ frame .setSize (200 , 200 );
26
+ frame .setLocationRelativeTo (null );
27
+ frame .setVisible (true );
28
+ }
29
+
30
+ private static void createUI (JFrame frame ) {
31
+ JPanel panel = new JPanel ();
32
+ Calculator calculator = new Calculator ();
33
+ GridBagLayout layout = new GridBagLayout ();
34
+ GridBagConstraints gbc = new GridBagConstraints ();
35
+ panel .setLayout (layout );
36
+
37
+ inputBox = new JTextField (10 );
38
+ inputBox .setEditable (false );
39
+
40
+ JButton button0 = new JButton ("0" );JButton button1 = new JButton ("1" );
41
+ JButton button2 = new JButton ("2" );JButton button3 = new JButton ("3" );
42
+ JButton button4 = new JButton ("4" );JButton button5 = new JButton ("5" );
43
+ JButton button6 = new JButton ("6" );JButton button7 = new JButton ("7" );
44
+ JButton button8 = new JButton ("8" );JButton button9 = new JButton ("9" );
45
+
46
+ JButton buttonPlus = new JButton ("+" );JButton buttonMinus = new JButton ("-" );
47
+ JButton buttonDivide = new JButton ("/" );JButton buttonMultiply = new JButton ("x" );
48
+ JButton buttonClear = new JButton ("C" );JButton buttonDot = new JButton ("." );
49
+ JButton buttonEquals = new JButton ("=" );
50
+
51
+ button1 .addActionListener (calculator );
52
+ button2 .addActionListener (calculator );
53
+ button3 .addActionListener (calculator );
54
+ button4 .addActionListener (calculator );
55
+ button5 .addActionListener (calculator );
56
+ button6 .addActionListener (calculator );
57
+ button7 .addActionListener (calculator );
58
+ button8 .addActionListener (calculator );
59
+ button9 .addActionListener (calculator );
60
+ button0 .addActionListener (calculator );
61
+
62
+ buttonPlus .addActionListener (calculator );buttonMinus .addActionListener (calculator );
63
+ buttonDivide .addActionListener (calculator );buttonMultiply .addActionListener (calculator );
64
+ buttonClear .addActionListener (calculator );buttonDot .addActionListener (calculator );
65
+ buttonEquals .addActionListener (calculator );
66
+
67
+ gbc .fill = GridBagConstraints .HORIZONTAL ;
68
+ gbc .gridx = 0 ; gbc .gridy = 0 ; panel .add (button1 , gbc );
69
+ gbc .gridx = 1 ; gbc .gridy = 0 ; panel .add (button2 , gbc );
70
+ gbc .gridx = 2 ; gbc .gridy = 0 ; panel .add (button3 , gbc );
71
+ gbc .gridx = 3 ; gbc .gridy = 0 ; panel .add (buttonPlus , gbc );
72
+ gbc .gridx = 0 ; gbc .gridy = 1 ; panel .add (button4 , gbc );
73
+ gbc .gridx = 1 ; gbc .gridy = 1 ; panel .add (button5 , gbc );
74
+ gbc .gridx = 2 ; gbc .gridy = 1 ; panel .add (button6 , gbc );
75
+ gbc .gridx = 3 ; gbc .gridy = 1 ; panel .add (buttonMinus , gbc );
76
+ gbc .gridx = 0 ; gbc .gridy = 2 ; panel .add (button7 , gbc );
77
+ gbc .gridx = 1 ; gbc .gridy = 2 ; panel .add (button8 , gbc );
78
+ gbc .gridx = 2 ; gbc .gridy = 2 ; panel .add (button9 , gbc );
79
+ gbc .gridx = 3 ; gbc .gridy = 2 ; panel .add (buttonDivide , gbc );
80
+ gbc .gridx = 0 ; gbc .gridy = 3 ; panel .add (buttonDot , gbc );
81
+ gbc .gridx = 1 ; gbc .gridy = 3 ; panel .add (button0 , gbc );
82
+ gbc .gridx = 2 ; gbc .gridy = 3 ; panel .add (buttonClear , gbc );
83
+ gbc .gridx = 3 ; gbc .gridy = 3 ; panel .add (buttonMultiply , gbc );
84
+ gbc .gridwidth = 3 ;
85
+
86
+ gbc .gridx = 0 ; gbc .gridy = 4 ; panel .add (inputBox , gbc );
87
+ gbc .gridx = 3 ; gbc .gridy = 4 ; panel .add (buttonEquals , gbc );
88
+ frame .getContentPane ().add (panel , BorderLayout .CENTER );
89
+ }
90
+
91
+ public void actionPerformed (ActionEvent e ) {
92
+ String command = e .getActionCommand ();
93
+ if (command .charAt (0 ) == 'C' ) {
94
+ inputBox .setText ("" );
95
+ }else if (command .charAt (0 ) == '=' ) {
96
+ inputBox .setText (evaluate (inputBox .getText ()));
97
+ }else {
98
+ inputBox .setText (inputBox .getText () + command );
99
+ }
100
+ }
101
+
102
+ public static String evaluate (String expression ) {
103
+ char [] arr = expression .toCharArray ();
104
+ String operand1 = "" ;String operand2 = "" ;String operator = "" ;
105
+ double result = 0 ;
106
+
107
+ for (int i = 0 ; i < arr .length ; i ++) {
108
+ if (arr [i ] >= '0' && arr [i ] <= '9' || arr [i ] == '.' ) {
109
+ if (operator .isEmpty ()){
110
+ operand1 += arr [i ];
111
+ }else {
112
+ operand2 += arr [i ];
113
+ }
114
+ }
115
+
116
+ if (arr [i ] == '+' || arr [i ] == '-' || arr [i ] == '/' || arr [i ] == '*' ) {
117
+ operator += arr [i ];
118
+ }
119
+ }
120
+
121
+ if (operator .equals ("+" ))
122
+ result = (Double .parseDouble (operand1 ) + Double .parseDouble (operand2 ));
123
+ else if (operator .equals ("-" ))
124
+ result = (Double .parseDouble (operand1 ) - Double .parseDouble (operand2 ));
125
+ else if (operator .equals ("/" ))
126
+ result = (Double .parseDouble (operand1 ) / Double .parseDouble (operand2 ));
127
+ else
128
+ result = (Double .parseDouble (operand1 ) * Double .parseDouble (operand2 ));
129
+ return operand1 + operator + operand2 + "=" +result ;
130
+ }
131
+ }
0 commit comments