-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvalExprGUI.java
158 lines (127 loc) · 3.98 KB
/
EvalExprGUI.java
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package evalExpr;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.event.*;
public class EvalExprGUI {
public static void main(String[] args) {
EvalExprFrame f= new EvalExprFrame();
f.setVisible(true);
}//main
}//EvalExprGUI
class EvalExprFrame extends JFrame implements ActionListener {
private JButton vai, cancella;
private JTextField expr, res;
private JLabel lblExpr, lblRes;
private JMenuItem exit, about;
public EvalExprFrame() {
setTitle("Risolutore Espressione Aritmetica");
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocation(400,400);
setSize(400,200);
JMenuBar mb= new JMenuBar();
setJMenuBar(mb);
JMenu file= new JMenu("File");
JMenu help= new JMenu( "Help");
file.addActionListener(this);
help.addActionListener(this);
mb.add(file);
mb.add(help);
exit= new JMenuItem("Exit");
about= new JMenuItem( "About");
exit.addActionListener(this);
about.addActionListener(this);
file.add(exit);
help.add(about);
JPanel p= new JPanel();
p.setBackground(Color.MAGENTA);
p.setBorder(new LineBorder(Color.BLACK, 1));
lblExpr= new JLabel("Fornisci l'espressione: ");
lblExpr.setFont(new Font("Verdana", Font.PLAIN, 15));
expr= new JTextField(23);
expr.setEditable(true);
expr.setFont(new Font("Verdana", Font.PLAIN, 15));
vai= new JButton( "Vai");
cancella= new JButton( "Cancella" );
expr.addActionListener(this);
vai.addActionListener(this);
cancella.addActionListener(this);
p.add(lblExpr);
p.add(expr);
p.add(vai);
p.add(cancella);
add(p, BorderLayout.CENTER);
JPanel q= new JPanel();
q.setBackground(Color.CYAN);
q.setBorder(new LineBorder(Color.BLACK, 2));
lblRes= new JLabel("Risultato: ");
res= new JTextField(15);
res.setEditable(false);
res.addActionListener(this);
q.add(lblRes);
q.add(res);
add(q, BorderLayout.SOUTH);
}//costruttore
public void actionPerformed(ActionEvent evt) {
if( evt.getSource()== vai) {
String s= expr.getText();
String pattern= "(\\d+|[\\+\\-\\/*\\^\\(\\)])+";
JOptionPane e= new JOptionPane
("Espressione Malformata.", JOptionPane.ERROR_MESSAGE) ;
if( !s.matches(pattern)) {
JOptionPane.showMessageDialog(null, "Espressione malformata!",
"ERROR", JOptionPane.ERROR_MESSAGE);
expr.setText("");
expr.setEditable(true);
}
else {
int ris= EvalExpr.evaluate(s);
res.setText(Integer.toString(ris));
return;
}
}//vai
else if( evt.getSource()== cancella ) {
expr.setText("");
expr.setEditable(true);
res.setText("");
res.setEditable(false);
}//cancella
else if( evt.getSource()==exit ) {
System.exit(0);
}//exit
else if( evt.getSource()== about ) {
JFrame abF= new AboutFrame();
abF.setVisible(true);
}//about
}//actionPerformed
class AboutFrame extends JFrame{
public AboutFrame() {
setTitle("Help");
setSize(500,300);
setLocation(710,300);
setResizable(false);
JPanel aboutPanel= new JPanel();
aboutPanel.setOpaque(true);
add(aboutPanel);
JTextArea txtArea= new JTextArea();
txtArea.setBackground(Color.LIGHT_GRAY);
txtArea.setFont( new Font("Consolas", Font.PLAIN, 10));
txtArea.setPreferredSize((new Dimension(250,200)));
txtArea.setEditable(false);
txtArea.append("\n"
+" This simple program \n"
+ " evaluates an arithmetic expression \n"
+ " using the PEMDAS rule: \n"
+ "\n"
+" P- Parenthesis \n"
+" E- Exponent \n"
+" MD- Multiplication,Modulo,Division \n"
+" AS- Addition, Subtraction \n"
+ "\n"
+ "-Jhenalyn Subol,213485");
aboutPanel.add(txtArea);
pack();
}//costruttore
}//AboutFrame
}//EvalExprFrame