Krolyxon
Commit d179508

Branches

Refs →

jcalGFrame.java

177 lines · 6.1 KB · Java

1/*
2* TODO:
3* Item positions are hard coded, make them dynamic
4* so that window can be set to resizable.
5*/
6
7
8import java.util.Vector;
9import javax.swing.*;
10import java.awt.Color;
11import java.awt.Dimension;
12import java.awt.Font;
13import java.awt.Insets;
14import java.awt.event.*;
15
16public class GFrame extends JFrame {
17 Parser p;
18 JTextField tf;
19
20 private JButton newButton(String text, int d1, int d2, int d3, int d4) {
21 JButton b = new JButton();
22 // b.setBackground(Color.decode("#F78361"));
23 b.setBackground(Color.decode("#2B2B2B"));
24 b.setForeground(Color.decode("#e5e5e5"));
25 b.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
26 b.setBounds(d1, d2, d3, d4);
27 b.setText(text);
28 return b;
29 }
30
31 private void actionAdderForTextField(JButton b, String val) {
32 b.addActionListener(new ActionListener() {
33 public void actionPerformed(ActionEvent e) {
34 tf.setText(tf.getText() + val);
35 }
36 });
37
38 }
39
40 GFrame(String title) {
41 History history = new History();
42 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
43 this.setTitle(title);
44 this.getContentPane().setBackground(Color.decode("#111111"));
45 this.setResizable(false);
46
47 // Text Field
48 tf = new JTextField();
49 tf.setBackground(Color.decode("#2B2B2B"));
50 tf.setForeground(Color.decode("#e5e5e5"));
51 tf.setMargin(new Insets(0, 10, 0, 10));
52 tf.setBounds(40, 50, 310, 40);
53 tf.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
54
55 // History Text Field
56 JTextField tHist = new JTextField();
57 tHist.setBounds(100, 10, 310, 40);
58 tHist.setEditable(false);
59
60 // Buttons
61 JButton bEval = newButton("=", 250, 100, 100, 40);
62 JButton bClear = newButton("CL", 180, 100, 60, 40);
63 JButton bAdd = newButton("+", 250, 300, 100, 40);
64 JButton bSub = newButton("-", 250, 250, 100, 40);
65 JButton bMul = newButton("X", 250, 200, 100, 40);
66 JButton bDiv = newButton("%", 250, 150, 100, 40);
67 JButton bCut = newButton("", 180, 300, 60, 40);
68 bCut.setFont(new Font("JetBrainsMono Nerd Font", Font.PLAIN, 20 ));
69 JButton bRightPar = newButton(")", 110, 100, 60, 40);
70 JButton bLeftPar = newButton("(", 40, 100, 60, 40);
71 JButton bDoubleZero = newButton("00", 40, 300, 60, 40);
72 JButton bZero = newButton("0", 110, 300, 60, 40);
73 JButton bOne = newButton("1", 40, 250, 60, 40);
74 JButton bTwo = newButton("2", 110, 250, 60, 40);
75 JButton bThree = newButton("3", 180, 250, 60, 40);
76 JButton bFour = newButton("4", 40, 200, 60, 40);
77 JButton bFive = newButton("5", 110, 200, 60, 40);
78 JButton bSix = newButton("6", 180, 200, 60, 40);
79 JButton bSeven = newButton("7", 40, 150, 60, 40);
80 JButton bEight = newButton("8", 110, 150, 60, 40);
81 JButton bNine = newButton("9", 180, 150, 60, 40);
82
83 // Add the buttons to the Frame
84 this.add(bEval);
85 this.add(bClear);
86 this.add(bAdd);
87 this.add(bSub);
88 this.add(bMul);
89 this.add(bDiv);
90 this.add(bCut);
91 this.add(bRightPar);
92 this.add(bLeftPar);
93 this.add(bDoubleZero);
94 this.add(bZero);
95 this.add(bOne);
96 this.add(bTwo);
97 this.add(bThree);
98 this.add(bFour);
99 this.add(bFive);
100 this.add(bSix);
101 this.add(bSeven);
102 this.add(bEight);
103 this.add(bNine);
104 this.add(tf);
105 // this.add(tHist);
106 this.setSize(400, 400);
107 this.setLayout(null);
108 this.setVisible(true);
109
110 JLabel label = new JLabel("Enter Expression: ");
111 label.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
112 label.setBounds(40, 20, 150, 40);
113 label.setForeground(Color.decode("#e5e5e5"));
114 this.add(label);
115
116 // ActionsListeners
117 bEval.addActionListener(new ActionListener() {
118 public void actionPerformed(ActionEvent e) {
119 if (tf.getText().length() != 0) {
120 p = new Parser(tf.getText());
121 tf.setText(Double.toString(p.eval()));
122
123 // Save to history
124 history.addHistory(tf.getText());
125
126 tHist.setText("");
127 Vector<String> s = history.getHistory();
128 for (int i = 0; i < s.size(); i++) {
129 System.out.println(s.get(i));
130 tHist.setText(new String(s.get(i).concat(tHist.getText())).concat("\n"));
131 }
132
133 } else {
134 tf.setText("No input");
135 }
136 }
137 });
138
139 // Common actions that just appends the symbols to the text field
140 actionAdderForTextField(bAdd, "+");
141 actionAdderForTextField(bSub, "-");
142 actionAdderForTextField(bMul, "*");
143 actionAdderForTextField(bDiv, "/");
144 actionAdderForTextField(bRightPar, ")");
145 actionAdderForTextField(bLeftPar, "(");
146 actionAdderForTextField(bDoubleZero, "00");
147 actionAdderForTextField(bZero, "0");
148 actionAdderForTextField(bOne, "1");
149 actionAdderForTextField(bTwo, "2");
150 actionAdderForTextField(bThree, "3");
151 actionAdderForTextField(bFour, "4");
152 actionAdderForTextField(bFive, "5");
153 actionAdderForTextField(bSix, "6");
154 actionAdderForTextField(bSeven, "7");
155 actionAdderForTextField(bEight, "8");
156 actionAdderForTextField(bNine, "9");
157
158 bClear.addActionListener(new ActionListener() {
159 public void actionPerformed(ActionEvent e) {
160 tf.setText("");
161 }
162 });
163
164 bCut.addActionListener(new ActionListener() {
165 public void actionPerformed(ActionEvent e) {
166 String text = tf.getText();
167 tf.setText(text.substring(0, text.length() - 1));
168 }
169 });
170
171 // bOne.addActionListener(new ActionListener() {
172 // public void actionPerformed(ActionEvent e) {
173 // tf.setText(tf.getText() + "1");
174 // }
175 // });
176 }
177}