- //Julius Medrano
- //Lab 08, Calculator
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Calculator
- {
- ButtonActionListener BAL = new ButtonActionListener();
- JTextField input = new JTextField("0", 27);
- boolean newNumber = true;
- JButton one = new JButton("1");
- JButton cButton = new JButton("C");
- JButton CeButton = new JButton("CE");
- JButton plusMin = new JButton("+/-");
- JButton div = new JButton("/");
- JButton seven = new JButton("7");
- JButton eight = new JButton("8");
- JButton nine = new JButton("9");
- JButton mult = new JButton("*");
- JButton four = new JButton("4");
- JButton five = new JButton("5");
- JButton six = new JButton("6");
- JButton sub = new JButton("-");
- JButton two = new JButton("2");
- JButton three = new JButton("3");
- JButton plus = new JButton("+");
- JButton zero = new JButton("0");
- JButton dot = new JButton(".");
- JButton equal = new JButton("=");
- void addDigit(int i)
- {
- if(newNumber)
- {
- input.setText(""+i);
- newNumber = false;
- }
- else
- {
- input.setText(input.getText()+i);
- }
- }
- class ButtonActionListener implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource().equals(one))
- input.setText("1");
- if(e.getSource().equals(two))
- input.setText("2");
- if(e.getSource().equals(three))
- input.setText("3");
- if(e.getSource().equals(four))
- input.setText("4");
- if(e.getSource().equals(five))
- input.setText("5");
- if(e.getSource().equals(six))
- input.setText("6");
- if(e.getSource().equals(seven))
- input.setText("7");
- if(e.getSource().equals(eight))
- input.setText("8");
- if(e.getSource().equals(nine))
- input.setText("9");
- if(e.getSource().equals(zero))
- input.setText("0");
- }
- public static void main(String[] args)
- {
- ButtonActionListener BAL = new ButtonActionListener();
- JFrame frm = new JFrame("Calculator Lab");
- JPanel buttons = new JPanel();
- JPanel textLabel = new JPanel();
- JPanel buttons0 = new JPanel();
- JPanel buttonsEtc = new JPanel();
- input.setHorizontalAlignment(JTextField.RIGHT);
- input.addActionListener(BAL);
- input.setEditable(false);
- JButton cButton = new JButton("C");
- JButton CeButton = new JButton("CE");
- JButton plusMin = new JButton("+/-");
- JButton div = new JButton("/");
- JButton seven = new JButton("7");
- JButton eight = new JButton("8");
- JButton nine = new JButton("9");
- JButton mult = new JButton("*");
- JButton four = new JButton("4");
- JButton five = new JButton("5");
- JButton six = new JButton("6");
- JButton sub = new JButton("-");
- JButton two = new JButton("2");
- JButton three = new JButton("3");
- JButton plus = new JButton("+");
- JButton zero = new JButton("0");
- JButton dot = new JButton(".");
- JButton equal = new JButton("=");
- double numOne = 0;
- double numTwo = 0;
- textLabel.setLayout(new BorderLayout());
- textLabel.add(input, BorderLayout.NORTH);
- buttons.setLayout(new GridLayout(4,4));
- buttons.add(cButton);
- cButton.addActionListener(BAL);
- buttons.add(CeButton);
- CeButton.addActionListener(BAL);
- buttons.add(plusMin);
- plusMin.addActionListener(BAL);
- buttons.add(div);
- div.addActionListener(BAL);
- buttons.add(seven);
- seven.addActionListener(BAL);
- buttons.add(eight);
- eight.addActionListener(BAL);
- buttons.add(nine);
- nine.addActionListener(BAL);
- buttons.add(mult);
- mult.addActionListener(BAL);
- buttons.add(four);
- four.addActionListener(BAL);
- buttons.add(five);
- five.addActionListener(BAL);
- buttons.add(six);
- six.addActionListener(BAL);
- buttons.add(sub);
- sub.addActionListener(BAL);
- buttons.add(one);
- one.addActionListener(BAL);
- buttons.add(two);
- two.addActionListener(BAL);
- buttons.add(three);
- three.addActionListener(BAL);
- buttons.add(plus);
- plus.addActionListener(BAL);
- buttons0.setLayout(new GridLayout(1,2));
- buttons0.add(zero);
- zero.addActionListener(BAL);
- buttonsEtc.setLayout(new GridLayout(1,2));
- buttonsEtc.add(dot);
- dot.addActionListener(BAL);
- buttonsEtc.add(equal);
- equal.addActionListener(BAL);
- buttons0.add(buttonsEtc);
- Container contentPane = frm.getContentPane();
- contentPane.setLayout(new BorderLayout());
- contentPane.add(textLabel, BorderLayout.NORTH);
- contentPane.add(buttons, BorderLayout.CENTER);
- contentPane.add(buttons0, BorderLayout.SOUTH);
- frm.pack();
- frm.setSize(300,200);
- //frm.setResizable(false);
- frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frm.setVisible(true);
- }
- }
- }