| 1 | import javax.swing.*; |
| 2 | import java.awt.Color; |
| 3 | import java.awt.Dimension; |
| 4 | import java.awt.event.*; |
| 5 | |
| 6 | public class GFrame extends JFrame { |
| 7 | Parser p; |
| 8 | |
| 9 | GFrame(String title) { |
| 10 | this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 11 | this.setTitle(title); |
| 12 | this.getContentPane().setBackground(Color.decode("#4E586e")); |
| 13 | JButton b = new JButton(); |
| 14 | b.setBounds(130, 100, 100, 40); |
| 15 | b.setBackground(Color.decode("#F78361")); |
| 16 | b.setText("Evaluate"); |
| 17 | |
| 18 | JTextField tf = new JTextField(); |
| 19 | tf.setBounds(130, 50, 220, 40); |
| 20 | |
| 21 | JButton bClear = new JButton(); |
| 22 | bClear.setBounds(250, 100, 100, 40); |
| 23 | bClear.setBackground(Color.decode("#F78361")); |
| 24 | bClear.setText("Clear"); |
| 25 | |
| 26 | this.add(b); |
| 27 | this.add(bClear); |
| 28 | this.add(tf); |
| 29 | this.setSize(400, 500); |
| 30 | this.setLayout(null); |
| 31 | this.setVisible(true); |
| 32 | |
| 33 | JLabel label = new JLabel("Enter Expression: "); |
| 34 | label.setBounds(130, 20, 150, 40); |
| 35 | this.add(label); |
| 36 | |
| 37 | b.addActionListener(new ActionListener() { |
| 38 | public void actionPerformed(ActionEvent e) { |
| 39 | if (tf.getText().length() != 0) { |
| 40 | p = new Parser(tf.getText()); |
| 41 | tf.setText(Double.toString(p.eval())); |
| 42 | } else { |
| 43 | tf.setText("No input"); |
| 44 | } |
| 45 | } |
| 46 | }); |
| 47 | |
| 48 | bClear.addActionListener(new ActionListener() { |
| 49 | public void actionPerformed(ActionEvent e) { |
| 50 | tf.setText(""); |
| 51 | } |
| 52 | }); |
| 53 | } |
| 54 | } |