-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
145 lines (129 loc) · 4.32 KB
/
GUI.java
File metadata and controls
145 lines (129 loc) · 4.32 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
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
public class GUI {
private JFrame frame;
private Calculator calc;
private JTextField input;
private JLabel output;
private JTextArea error;
private JScrollPane errorScroll;
private JButton plus;
private JButton minus;
private JButton times;
private JButton divide;
private JButton sin;
private JButton cos;
private JButton tan;
private JButton inv;
private JButton lparen;
private JButton rparen;
private JButton clearError;
public GUI() {
calc = new Calculator();
frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(825,300));
JComponent c = (JComponent) frame.getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
input = new JTextField(72);
input.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
if (e.getDot() == 0 && e.getMark() == 0) {
// return to start of text to overwrite text
} else if (e.getDot() != e.getMark()) {
// text is selected
} else {
// new text written
calc.setInput(input.getText());
output.setText(String.valueOf(calc.getOutput()));
error.append(calc.getError());
}
}
});
output = new JLabel("0.");
output.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 32));
output.setPreferredSize(new Dimension(800, 100));
error = new JTextArea(5, 80);
error.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
error.setEditable(false);
errorScroll = new JScrollPane(error);
errorScroll
.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
plus = genButton("+");
minus = genButton("-");
times = genButton("*");
divide = genButton("/");
sin = genButton("sin(");
cos = genButton("cos(");
tan = genButton("tan(");
inv = genButton("1/(");
lparen = genButton("(");
rparen = genButton(")");
clearError = new JButton("Clear Error");
clearError.setPreferredSize(new Dimension(160,50));
clearError.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
error.setText("");
calc.clearError();
}
});
c.add(input);
c.add(plus);
c.add(minus);
c.add(times);
c.add(divide);
c.add(sin);
c.add(cos);
c.add(tan);
c.add(inv);
c.add(lparen);
c.add(rparen);
c.add(output);
c.add(errorScroll);
c.add(clearError);
frame.addWindowFocusListener(new WindowAdapter() {
public void windowGainedFocus(WindowEvent e) {
input.requestFocusInWindow();
}
});
frame.pack();
}
private JButton genButton(String label) {
JButton b = new JButton(label);
b.setPreferredSize(new Dimension(80,50));
b.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
input.setText(input.getText() + " " + label + " ");
input.requestFocusInWindow();
}
});
return b;
}
public void show() {
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new GUI().show();
}
});
}
}