jcal991282c
add History support and make buttons rounded
+46 −15
2 files changed
- M GFrame.java +46 −12
- M History.java +0 −3
| @@ -4,7 +4,6 @@ | |||
| 4 | 4 | * so that window can be set to resizable. | |
| 5 | 5 | */ | |
| 6 | 6 | ||
| 7 | − | ||
| 8 | 7 | import java.util.Vector; | |
| 9 | 8 | import javax.swing.*; | |
| 10 | 9 | import java.awt.Color; | |
| @@ -12,6 +11,30 @@ import java.awt.Dimension; | |||
| 12 | 11 | import java.awt.Font; | |
| 13 | 12 | import java.awt.Insets; | |
| 14 | 13 | import java.awt.event.*; | |
| 14 | + | import java.awt.*; | |
| 15 | + | import javax.swing.border.Border; | |
| 16 | + | import java.text.DecimalFormat; | |
| 17 | + | ||
| 18 | + | class RoundBtn implements Border { | |
| 19 | + | private int r; | |
| 20 | + | ||
| 21 | + | RoundBtn(int r) { | |
| 22 | + | this.r = r; | |
| 23 | + | } | |
| 24 | + | ||
| 25 | + | public Insets getBorderInsets(Component c) { | |
| 26 | + | return new Insets(this.r + 1, this.r + 1, this.r + 2, this.r); | |
| 27 | + | } | |
| 28 | + | ||
| 29 | + | public boolean isBorderOpaque() { | |
| 30 | + | return true; | |
| 31 | + | } | |
| 32 | + | ||
| 33 | + | public void paintBorder(Component c, Graphics g, int x, int y, | |
| 34 | + | int width, int height) { | |
| 35 | + | g.drawRoundRect(x, y, width - 1, height - 1, r, r); | |
| 36 | + | } | |
| 37 | + | } | |
| 15 | 38 | ||
| 16 | 39 | public class GFrame extends JFrame { | |
| 17 | 40 | Parser p; | |
| @@ -24,6 +47,7 @@ public class GFrame extends JFrame { | |||
| 24 | 47 | b.setForeground(Color.decode("#e5e5e5")); | |
| 25 | 48 | b.setFont((new Font("Times New Roman", Font.PLAIN, 20))); | |
| 26 | 49 | b.setBounds(d1, d2, d3, d4); | |
| 50 | + | b.setBorder(new RoundBtn(15)); | |
| 27 | 51 | b.setText(text); | |
| 28 | 52 | return b; | |
| 29 | 53 | } | |
| @@ -38,7 +62,6 @@ public class GFrame extends JFrame { | |||
| 38 | 62 | } | |
| 39 | 63 | ||
| 40 | 64 | GFrame(String title) { | |
| 41 | − | History history = new History(); | |
| 42 | 65 | this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| 43 | 66 | this.setTitle(title); | |
| 44 | 67 | this.getContentPane().setBackground(Color.decode("#111111")); | |
| @@ -51,11 +74,19 @@ public class GFrame extends JFrame { | |||
| 51 | 74 | tf.setMargin(new Insets(0, 10, 0, 10)); | |
| 52 | 75 | tf.setBounds(40, 50, 310, 40); | |
| 53 | 76 | tf.setFont((new Font("Times New Roman", Font.PLAIN, 20))); | |
| 77 | + | tf.setBorder(new RoundBtn(5)); | |
| 54 | 78 | ||
| 55 | 79 | // History Text Field | |
| 56 | − | JTextField tHist = new JTextField(); | |
| 57 | − | tHist.setBounds(100, 10, 310, 40); | |
| 80 | + | History history = new History(); | |
| 81 | + | JTextArea tHist = new JTextArea(); | |
| 82 | + | tHist.setBounds(380, 50, 200, 290); | |
| 58 | 83 | tHist.setEditable(false); | |
| 84 | + | tHist.setBorder(new RoundBtn(5)); | |
| 85 | + | tHist.setBackground(Color.decode("#2B2B2B")); | |
| 86 | + | tHist.setForeground(Color.decode("#e5e5e5")); | |
| 87 | + | tHist.setFont((new Font("Times New Roman", Font.PLAIN, 20))); | |
| 88 | + | tHist.setBorder(new RoundBtn(5)); | |
| 89 | + | tHist.setMargin(new Insets(0, 10, 0, 10)); | |
| 59 | 90 | ||
| 60 | 91 | // Buttons | |
| 61 | 92 | JButton bEval = newButton("=", 250, 100, 100, 40); | |
| @@ -65,7 +96,7 @@ public class GFrame extends JFrame { | |||
| 65 | 96 | JButton bMul = newButton("X", 250, 200, 100, 40); | |
| 66 | 97 | JButton bDiv = newButton("%", 250, 150, 100, 40); | |
| 67 | 98 | JButton bCut = newButton("", 180, 300, 60, 40); | |
| 68 | − | bCut.setFont(new Font("JetBrainsMono Nerd Font", Font.PLAIN, 20 )); | |
| 99 | + | bCut.setFont(new Font("JetBrainsMono Nerd Font", Font.PLAIN, 20)); | |
| 69 | 100 | JButton bRightPar = newButton(")", 110, 100, 60, 40); | |
| 70 | 101 | JButton bLeftPar = newButton("(", 40, 100, 60, 40); | |
| 71 | 102 | JButton bDoubleZero = newButton("00", 40, 300, 60, 40); | |
| @@ -102,8 +133,8 @@ public class GFrame extends JFrame { | |||
| 102 | 133 | this.add(bEight); | |
| 103 | 134 | this.add(bNine); | |
| 104 | 135 | this.add(tf); | |
| 105 | − | // this.add(tHist); | |
| 106 | − | this.setSize(400, 400); | |
| 136 | + | this.add(tHist); | |
| 137 | + | this.setSize(600, 400); | |
| 107 | 138 | this.setLayout(null); | |
| 108 | 139 | this.setVisible(true); | |
| 109 | 140 | ||
| @@ -117,17 +148,20 @@ public class GFrame extends JFrame { | |||
| 117 | 148 | bEval.addActionListener(new ActionListener() { | |
| 118 | 149 | public void actionPerformed(ActionEvent e) { | |
| 119 | 150 | if (tf.getText().length() != 0) { | |
| 151 | + | String expression = tf.getText(); | |
| 152 | + | DecimalFormat format = new DecimalFormat(); | |
| 120 | 153 | p = new Parser(tf.getText()); | |
| 121 | − | tf.setText(Double.toString(p.eval())); | |
| 122 | − | ||
| 123 | − | // Save to history | |
| 124 | − | history.addHistory(tf.getText()); | |
| 154 | + | String formattedResult = format.format(p.eval()); | |
| 155 | + | tf.setText(formattedResult); | |
| 125 | 156 | ||
| 126 | 157 | tHist.setText(""); | |
| 127 | 158 | Vector<String> s = history.getHistory(); | |
| 159 | + | s.add(expression + " = " + formattedResult); | |
| 128 | 160 | for (int i = 0; i < s.size(); i++) { | |
| 129 | 161 | System.out.println(s.get(i)); | |
| 130 | − | tHist.setText(new String(s.get(i).concat(tHist.getText())).concat("\n")); | |
| 162 | + | // tHist.setText(new String(s.get(i).concat(tHist.getText())).concat("\n")); | |
| 163 | + | tHist.append(s.get(i)); | |
| 164 | + | tHist.append("\n"); | |
| 131 | 165 | } | |
| 132 | 166 | ||
| 133 | 167 | } else { | |
| @@ -7,9 +7,6 @@ public class History { | |||
| 7 | 7 | hist = new Vector<String>(); | |
| 8 | 8 | } | |
| 9 | 9 | ||
| 10 | − | public void addHistory(String h) { | |
| 11 | − | hist.add(h); | |
| 12 | − | } | |
| 13 | 10 | public Vector<String> getHistory() { | |
| 14 | 11 | return hist; | |
| 15 | 12 | } | |