-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojTwoFrame.java
More file actions
151 lines (133 loc) · 5.1 KB
/
projTwoFrame.java
File metadata and controls
151 lines (133 loc) · 5.1 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
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
/**
* This class contains all the code to make up the GUI for the postfix expression calculator
* @author Jonny Pine
* @version 1.0
*/
import javax.swing.*;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.*;
import java.awt.*;
public class projTwoFrame extends JFrame {
private JPanel contentPane;
private JTextField textFieldInput;
private JTextField textFieldOutput;
private Postfix evaluate;
/**
* Create the frame.
*/
public projTwoFrame() {
evaluate = new Postfix();
/**setting up the layout of the content pane**/
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 495, 114);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{88, 86, 41, 39, 30, 86, 0};
gbl_contentPane.rowHeights = new int[]{22, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
/**this sets up the label "postfix expression"**/
JLabel lblPostfixExpression = new JLabel("Postfix Expression");
lblPostfixExpression.setHorizontalAlignment(SwingConstants.CENTER);
GridBagConstraints gbc_lblPostfixExpression = new GridBagConstraints();
gbc_lblPostfixExpression.anchor = GridBagConstraints.WEST;
gbc_lblPostfixExpression.insets = new Insets(0, 0, 5, 5);
gbc_lblPostfixExpression.gridx = 0;
gbc_lblPostfixExpression.gridy = 0;
contentPane.add(lblPostfixExpression, gbc_lblPostfixExpression);
/**this input field**/
textFieldInput = new JTextField();
GridBagConstraints gbc_textFieldInput = new GridBagConstraints();
gbc_textFieldInput.anchor = GridBagConstraints.WEST;
gbc_textFieldInput.insets = new Insets(0, 0, 5, 5);
gbc_textFieldInput.gridx = 1;
gbc_textFieldInput.gridy = 0;
textFieldInput.addKeyListener(new KeyListener(){
/**this method associated with this key listener clears the text in the output box**/
public void keyPressed(KeyEvent arg0) {
textFieldOutput.setText("");
}
public void keyReleased(KeyEvent arg0) {
// required method
}
public void keyTyped(KeyEvent arg0) {
// required method
}
});
contentPane.add(textFieldInput, gbc_textFieldInput);
textFieldInput.setColumns(10);
Button button_1 = new Button("Solve");
button_1.addActionListener(new ActionListener(){
/**this method preforms the action event associated with this button**/
public void actionPerformed(ActionEvent e) {
try{
String expr = (textFieldInput.getText());
textFieldOutput.setText(String.valueOf(evaluate.evaluate(expr)));
}
catch (NumberFormatException nfe){
JOptionPane.showMessageDialog(null, "Your input is incorrectly typed");
}
}
});
GridBagConstraints gbc_button_1 = new GridBagConstraints();
gbc_button_1.anchor = GridBagConstraints.NORTHWEST;
gbc_button_1.insets = new Insets(0, 0, 5, 5);
gbc_button_1.gridx = 2;
gbc_button_1.gridy = 0;
contentPane.add(button_1, gbc_button_1);
/**creates a jlabel with the word result**/
JLabel lblResult = new JLabel("Result");
lblResult.setHorizontalAlignment(SwingConstants.CENTER);
GridBagConstraints gbc_lblResult = new GridBagConstraints();
gbc_lblResult.anchor = GridBagConstraints.WEST;
gbc_lblResult.insets = new Insets(0, 0, 0, 5);
gbc_lblResult.gridx = 0;
gbc_lblResult.gridy = 1;
contentPane.add(lblResult, gbc_lblResult);
textFieldOutput = new JTextField();
/**this defines the output field**/
GridBagConstraints gbc_textFieldOutput = new GridBagConstraints();
gbc_textFieldOutput.insets = new Insets(0, 0, 0, 5);
gbc_textFieldOutput.anchor = GridBagConstraints.WEST;
gbc_textFieldOutput.gridx = 1;
gbc_textFieldOutput.gridy = 1;
textFieldOutput.setEditable(false);
contentPane.add(textFieldOutput, gbc_textFieldOutput);
textFieldOutput.setColumns(10);
/**this adds an action listened to the button**/
Button button = new Button("Clear");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
textFieldInput.setText("");
textFieldOutput.setText("");
}
});
/**this specifications for the clear button**/
GridBagConstraints gbc_button = new GridBagConstraints();
gbc_button.anchor = GridBagConstraints.NORTHWEST;
gbc_button.insets = new Insets(0, 0, 0, 5);
gbc_button.gridx = 2;
gbc_button.gridy = 1;
contentPane.add(button, gbc_button);
}
/**
* The method for actually running the class and setting the visibility to true
**/
public void run() {
projTwoFrame frame = new projTwoFrame();
try {
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}