-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExprCalculator.java
More file actions
77 lines (59 loc) · 1.73 KB
/
Copy pathExprCalculator.java
File metadata and controls
77 lines (59 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package MyPack;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.*;
public class ExprCalculator extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JTextField tf;
JButton b;
JLabel l ;
JLabel res;
JPanel p;
Color c = new Color(255,0,0,100);
public ExprCalculator()
{ setLayout(null);
setSize(500,200);
JLabel l = new JLabel("Enter any algebric expressions for e.g 3 + ( 5 * 9 - 2 ) + 3 * 5");
p = new JPanel();
add(p);
p.setBackground(Color.blue);
b =new JButton("Evaluate");
tf= new JTextField(50);
res = new JLabel("Result : NA");
l.setBounds(50, 10, 400, 20);
tf.setBounds(50,40,350,30);
res.setBounds(50, 60, 400, 50);
b.setBounds(50,100,100,30);
add(l);
add(tf);
add(res);
add(b);
setVisible(true);
JOptionPane.showMessageDialog(this,"Warning! \n Remember to seperate each value/symbol by space.");
b.addActionListener(this); //ActionListener interface object
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
String expr =tf.getText();
int result = EvaluateString.evaluate(expr);
res.setText("Result : "+ result);
}
catch(Exception ee)
{
res.setText("Invalid data");
}
}
}