Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- public class Main {
- //Frame constructs
- public static JFrame frame;
- public static JPanel mainPanel;
- public static JPanel numbersPanel;
- public static JPanel buttonPanel;
- public static JTextArea output;
- public static JTextArea input;
- //numerical buttons
- public static JButton one;
- public static JButton two;
- public static JButton three;
- public static JButton four;
- public static JButton five;
- public static JButton six;
- public static JButton seven;
- public static JButton eight;
- public static JButton nine;
- public static JButton zero;
- //operator buttons
- public static JButton add;
- public static JButton subtract;
- public static JButton multiply;
- public static JButton divide;
- public static JButton clear;
- public static JButton clearAll;
- public static JButton decimal;
- //Action listener
- public static ActionListener buttonListener;
- //Doubles for storing math
- public static double numOne = 0;
- public static double numTwo = 0;
- public static void main(String[] args){
- //initialize all the components
- Initialize();
- //set up the GUI
- GUI();
- //update the button presses and do the math
- Update();
- }
- public static void Initialize(){
- //simple inits for frame, panels and text fields
- frame = new JFrame("Calculator");
- mainPanel = new JPanel();
- numbersPanel = new JPanel();
- buttonPanel = new JPanel();
- output = new JTextArea();
- input = new JTextArea();
- frame.setSize(300,380);
- frame.setResizable(false);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- mainPanel.setSize(300,380);
- numbersPanel.setPreferredSize(new Dimension(280,50));
- buttonPanel.setPreferredSize(new Dimension(290,380));
- output.setPreferredSize(new Dimension(280,20));
- output.setEditable(false);
- input.setPreferredSize(new Dimension(280,20));
- input.setEditable(false);
- //universal dimension to make it easier to change later
- Dimension buttonDimension = new Dimension(65,50);
- //set up dimensions for buttons
- //numerical buttons
- one = new JButton("1");
- one.setPreferredSize(buttonDimension);
- two = new JButton("2");
- two.setPreferredSize(buttonDimension);
- three = new JButton("3");
- three.setPreferredSize(buttonDimension);
- four = new JButton("4");
- four.setPreferredSize(buttonDimension);
- five = new JButton("5");
- five.setPreferredSize(buttonDimension);
- six = new JButton("6");
- six.setPreferredSize(buttonDimension);
- seven = new JButton("7");
- seven.setPreferredSize(buttonDimension);
- eight = new JButton("8");
- eight.setPreferredSize(buttonDimension);
- nine = new JButton("9");
- nine.setPreferredSize(buttonDimension);
- zero = new JButton("0");
- zero.setPreferredSize(buttonDimension);
- //operator buttons
- add = new JButton("+");
- add.setPreferredSize(buttonDimension);
- subtract = new JButton("-");
- subtract.setPreferredSize(buttonDimension);
- multiply = new JButton("x");
- multiply.setPreferredSize(buttonDimension);
- divide = new JButton("\\");
- divide.setPreferredSize(buttonDimension);
- clear = new JButton("C");
- clear.setPreferredSize(buttonDimension);
- clearAll = new JButton("Clear All");
- clearAll.setPreferredSize(new Dimension(100,50));
- decimal = new JButton(".");
- decimal.setPreferredSize(buttonDimension);
- }
- public static void GUI(){
- //add everything to the appropriate frame/panel
- frame.add(mainPanel);
- mainPanel.add(numbersPanel);
- mainPanel.add(buttonPanel);
- numbersPanel.add(output);
- numbersPanel.add(input);
- buttonPanel.add(one);
- buttonPanel.add(two);
- buttonPanel.add(three);
- buttonPanel.add(add);
- buttonPanel.add(four);
- buttonPanel.add(five);
- buttonPanel.add(six);
- buttonPanel.add(subtract);
- buttonPanel.add(seven);
- buttonPanel.add(eight);
- buttonPanel.add(nine);
- buttonPanel.add(multiply);
- buttonPanel.add(zero);
- buttonPanel.add(divide);
- buttonPanel.add(decimal);
- buttonPanel.add(clear);
- buttonPanel.add(clearAll);
- //make sure it's all visible
- frame.setVisible(true);
- }
- public static void Update(){
- //the action listener to control all the buttons
- buttonListener = new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- //input for numerical buttons
- if(e.getSource().equals(one))
- input.append("1");
- if(e.getSource().equals(two))
- input.append("2");
- if(e.getSource().equals(three))
- input.append("3");
- if(e.getSource().equals(four))
- input.append("4");
- if(e.getSource().equals(five))
- input.append("5");
- if(e.getSource().equals(six))
- input.append("6");
- if(e.getSource().equals(seven))
- input.append("7");
- if(e.getSource().equals(eight))
- input.append("8");
- if(e.getSource().equals(nine))
- input.append("9");
- if(e.getSource().equals(zero))
- input.append("0");
- //for adding decimals
- if(e.getSource().equals(decimal))
- input.append(".");
- //for inputting math operators
- //ADDING
- if(e.getSource().equals(add)){
- //don't deal with empty strings
- if(input.getText().equals(""))
- numOne = 0;
- else
- numOne = Double.parseDouble(input.getText());
- numTwo += numOne;
- //clear the input
- input.setText("");
- //display the sum
- output.setText("" + numTwo);
- }
- //SUBTRACTING
- if(e.getSource().equals(subtract)){
- //don't deal with empty strings
- if(input.getText().equals(""))
- numOne = 0;
- else
- numOne = Double.parseDouble(input.getText());
- numTwo -= numOne;
- //clear the input
- input.setText("");
- //display difference
- output.setText("" + numTwo);
- }
- //MULTIPLYING
- if(e.getSource().equals(multiply)){
- //don't deal with empty strings
- if(input.getText().equals(""))
- numOne = 0;
- else
- numOne = Double.parseDouble(input.getText());
- numTwo *= numOne;
- //clear the input
- input.setText("");
- //display product
- output.setText("" + numTwo);
- }
- //DIVIDING
- if(e.getSource().equals(divide)){
- //don't deal with empty strings
- if(input.getText().equals(""))
- numOne = 0;
- else
- numOne = Double.parseDouble(input.getText());
- numTwo /= numOne;
- //clear the input
- input.setText("");
- //display the quotient
- output.setText("" + numTwo);
- }
- //clear the input
- if(e.getSource().equals(clear)){
- //clear text field
- input.setText("");
- //clear number in memory
- numOne = 0;
- }
- //clear the whole calculator
- if(e.getSource().equals(clearAll)){
- //clear the text fields
- input.setText("");
- output.setText("");
- //clear the numbers in memory
- numOne = 0;
- numTwo = 0;
- }
- }
- };
- //add all the action listeners...
- //could probably house buttons in array and apply through that...
- one.addActionListener(buttonListener);
- two.addActionListener(buttonListener);
- three.addActionListener(buttonListener);
- four.addActionListener(buttonListener);
- five.addActionListener(buttonListener);
- six.addActionListener(buttonListener);
- seven.addActionListener(buttonListener);
- eight.addActionListener(buttonListener);
- nine.addActionListener(buttonListener);
- zero.addActionListener(buttonListener);
- add.addActionListener(buttonListener);
- subtract.addActionListener(buttonListener);
- multiply.addActionListener(buttonListener);
- divide.addActionListener(buttonListener);
- clear.addActionListener(buttonListener);
- clearAll.addActionListener(buttonListener);
- decimal.addActionListener(buttonListener);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment