-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostfixCalculatorGUI.java
59 lines (51 loc) · 2.19 KB
/
PostfixCalculatorGUI.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class PostfixCalculatorGUI {
private JFrame frame;
private JTextArea textArea;
private JTextField textField;
private PostOrderCalculator postOrderCalculator;
public PostfixCalculatorGUI() {
frame = new JFrame("Postfix Calculator");
textArea = new JTextArea();
textField = new JTextField();
postOrderCalculator = new PostOrderCalculator();
textArea.setText("Welcome to the Postfix Calculator!\n" +
"Please enter your postfix expressions in the text field below.\n" +
"For example, to add 1 and 2, type: '1 2 +'\n" +
"Then press Enter to calculate.\n funcstions include +, - , / , * , ^ , ~ (Root), S (Sin in radians), C (Cos in radians)\n"
+ "For sin or cosine just type any number after the base and select either operation \n");
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(400, 300));
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String input = textField.getText();
textField.setText("");
try {
String[] tokens = input.split("\\s+");
double result = postOrderCalculator.calculatePostOrder(tokens);
textArea.append(input + ": " + result + "\n");
} catch (IllegalArgumentException ex) {
textArea.append("Error: " + ex.getMessage() + "\n");
}
}
});
frame.setLayout(new BorderLayout());
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(textField, BorderLayout.SOUTH);
frame.pack();
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new PostfixCalculatorGUI();
}
}