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