Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.util.Vector;
- public class CalculatorLab extends JFrame implements ActionListener
- {
- private JPanel keypad;
- private JPanel inputs;
- private JTextField firstInput = new JTextField(25);
- private JTextField secondInput = new JTextField(25);
- private JTextField results = new JTextField(25);
- private JLabel operatorLbl = new JLabel("+");
- private JLabel equalsLbl = new JLabel("=");
- private String operator;
- private boolean first = true;
- private boolean second = false;
- public static void main(String[] args)
- {
- CalculatorLab frame = new CalculatorLab();
- frame.setSize(500,300);
- frame.setVisible(true);
- }
- public CalculatorLab()
- {
- super( "My Calculator" );
- init();
- }
- private void init()
- {
- setLayout( new BorderLayout() );
- keypad = new JPanel(new GridLayout(6,3));
- String[] labels = {"0","1","2","3","4","5","6","7","8","9","+","-","*","/","="," ","Clear"," "};
- for (int i = 0; i < labels.length; i++)
- {
- keypad.add(createButton(labels[i]));
- }
- add("East", keypad);
- JButton clear = new JButton("Clear");
- inputs = new JPanel(new GridLayout(4,2));
- inputs.add(new JLabel("Enter First Input:"));
- inputs.add(firstInput);
- operatorLbl.setHorizontalAlignment(SwingConstants.RIGHT);
- inputs.add(operatorLbl);
- inputs.add(new JLabel(""));
- inputs.add(new JLabel("Enter Second Input:"));
- inputs.add(secondInput);
- equalsLbl.setHorizontalAlignment(SwingConstants.RIGHT);
- inputs.add(equalsLbl);
- inputs.add(results);
- add("Center", inputs);
- clear.addActionListener(this);
- }
- private JButton createButton( String name )
- {
- JButton b = new JButton( name );
- b.addActionListener( this );
- return b;
- }
- public void actionPerformed( ActionEvent e )
- {
- String cmd = e.getActionCommand();
- if (cmd.equals("+")||cmd.equals("-")||cmd.equals("*")||cmd.equals("/"))
- {
- firstInput.setBackground(null);
- secondInput.setBackground(Color.YELLOW);
- operator = cmd;
- operatorLbl.setText(cmd);
- first = false;
- second = true;
- }
- else if (first)
- {
- firstInput.setBackground(Color.YELLOW);
- firstInput.setText(firstInput.getText()+cmd);
- results.setText("");
- }
- else if (cmd.equals("="))
- {
- secondInput.setBackground(null);
- results.setBackground(Color.YELLOW);
- second = false;
- results.setText(calculateResults());
- resetCalc();
- }
- else if (second)
- {
- secondInput.setText(secondInput.getText()+cmd);
- }
- else if (clear)
- }
- public String calculateResults()
- {
- double resultsValue = 0;
- if (operator.equals("+"))
- {
- resultsValue = Double.parseDouble(firstInput.getText()) + Double.parseDouble(secondInput.getText());
- }
- else if (operator.equals("-"))
- {
- resultsValue = Double.parseDouble(firstInput.getText()) - Double.parseDouble(secondInput.getText());
- }
- else if (operator.equals("*"))
- {
- resultsValue = Double.parseDouble(firstInput.getText()) * Double.parseDouble(secondInput.getText());
- }
- else if (operator.equals("/"))
- {
- resultsValue = Double.parseDouble(firstInput.getText()) / Double.parseDouble(secondInput.getText());
- }
- return Double.toString(resultsValue);
- }
- public void resetCalc()
- {
- firstInput.setText("");
- secondInput.setText("");
- first = true;
- second = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment