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.*;
- import javax.swing.border.Border;
- public class SciCalc {
- //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 equals;
- //other functions
- public static JButton clear;
- public static JButton clearAll;
- public static JButton decimal;
- public static JButton back;
- //Action listener
- public static ActionListener buttonListener;
- //Doubles for storing math
- public static double numOne = 0;
- public static double numTwo = 0;
- //String for displaying the operator
- public static String displayOperator = "";
- //Boolean for when to overwrite the current input with totally new input
- public static boolean overwrite = false;
- public static boolean firstInput = true;
- //JButton array
- public static JButton[] buttons;
- 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);
- //create a border for the numbersPanel
- Border inputOutputBorder = BorderFactory.createLineBorder(Color.black);
- numbersPanel.setPreferredSize(new Dimension(280,60));
- numbersPanel.setBackground(Color.white);
- numbersPanel.setBorder(inputOutputBorder);
- buttonPanel.setPreferredSize(new Dimension(290,380));
- output.setPreferredSize(new Dimension(268,19));
- output.setEditable(false);
- input.setPreferredSize(new Dimension(268,29));
- input.setEditable(false);
- //universal dimension to make it easier to change later
- Dimension buttonDimension = new Dimension(65,50);
- //create buttons
- one = new JButton("1");
- two = new JButton("2");
- three = new JButton("3");
- four = new JButton("4");
- five = new JButton("5");
- six = new JButton("6");
- seven = new JButton("7");
- eight = new JButton("8");
- nine = new JButton("9");
- zero = new JButton("0");
- //operator buttons
- add = new JButton("+");
- subtract = new JButton("-");
- multiply = new JButton("x");
- divide = new JButton("/");
- equals = new JButton("=");
- //other buttons
- clear = new JButton("C");
- clearAll = new JButton("Clear All");
- decimal = new JButton(".");
- back = new JButton("<==");
- //add buttons to buttons array
- buttons = new JButton[]{one,two,three,add,
- four,five,six,subtract,
- seven,eight,nine,multiply
- ,zero,decimal,clear,divide,
- clearAll, equals, back};
- //set dimensions
- for(int i=0; i < buttons.length; i++){
- buttons[i].setPreferredSize(buttonDimension);
- }
- clearAll.setPreferredSize(new Dimension(100,50));
- }
- 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);
- //add buttons to button panel
- for(int i = 0; i < buttons.length; i++){
- buttonPanel.add(buttons[i]);
- }
- //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))
- overwriteCheck(1);
- else if(e.getSource().equals(two))
- overwriteCheck(2);
- else if(e.getSource().equals(three))
- overwriteCheck(3);
- else if(e.getSource().equals(four))
- overwriteCheck(4);
- else if(e.getSource().equals(five))
- overwriteCheck(5);
- else if(e.getSource().equals(six))
- overwriteCheck(6);
- else if(e.getSource().equals(seven))
- overwriteCheck(7);
- else if(e.getSource().equals(eight))
- overwriteCheck(8);
- else if(e.getSource().equals(nine))
- overwriteCheck(9);
- else if(e.getSource().equals(zero))
- overwriteCheck(0);
- //for adding decimals
- else if(e.getSource().equals(decimal))
- input.append(".");
- //for inputting math operators
- else if(e.getSource().equals(add) || e.getSource().equals(subtract)
- || e.getSource().equals(multiply) || e.getSource().equals(divide)){
- //don't deal with empty strings
- if(!input.getText().equals(""))
- numOne = Double.parseDouble(input.getText());
- if(e.getSource().equals(add)){
- displayOperator = "+";
- numTwo += numOne;
- }
- else if(e.getSource().equals(subtract)){
- displayOperator = "-";
- if(firstInput)
- numTwo = numOne;
- else
- numTwo -= numOne;
- }
- else if(e.getSource().equals(multiply)){
- displayOperator = "*";
- if(firstInput)
- numTwo = numOne;
- else
- numTwo *= numOne;
- }
- else if(e.getSource().equals(divide)){
- displayOperator = "/";
- if(firstInput)
- numTwo = numOne;
- else
- numTwo /= numOne;
- }
- output.append(numOne + " " + displayOperator + " ");
- overwrite = true;
- firstInput = false;
- }
- //clear the input
- else if(e.getSource().equals(clear)){
- //clear text field
- input.setText("");
- //clear number in memory
- numOne = 0;
- }
- //clear the whole calculator
- else if(e.getSource().equals(clearAll)){
- //clear the text fields
- input.setText("");
- output.setText("");
- //clear the numbers in memory
- numOne = 0;
- numTwo = 0;
- }
- //Actually display the answer to the users
- else if(e.getSource().equals(equals)){
- //do last math
- String lastOperator = "";
- if(!output.getText().equals("")){
- lastOperator = output.getText();
- int length = output.getText().length();
- lastOperator = lastOperator.substring(length-2);
- lastOperator = lastOperator.trim();
- }
- System.out.println(lastOperator);
- //do the last bit of math with the input, depending on the last operator
- double tempInput;
- if(!input.getText().equals(null))
- tempInput = Double.parseDouble(input.getText());
- else
- tempInput = 0;
- if(lastOperator.equals("+"))
- numTwo += tempInput;
- else if(lastOperator.equals("-"))
- numTwo -= tempInput;
- else if(lastOperator.equals("*"))
- numTwo *= tempInput;
- else if(lastOperator.equals("/"))
- numTwo /= tempInput;
- //clear math
- output.setText("");
- //display answer in input
- input.setText("" + numTwo);
- //allow past input to be totally overwritten
- overwrite = true;
- firstInput = true;
- //clear internal values
- numOne = 0;
- numTwo = 0;
- }
- //delete last entered number
- else if(e.getSource().equals(back)){
- //copy the input for modification
- String changedInput = input.getText();
- //delete the last inputed character in the string
- if(!input.getText().equals(""))
- changedInput = changedInput.substring(0, changedInput.length()-1);
- //set the input to the reduced string
- input.setText(changedInput);
- }
- }
- };
- //add all the button listeners
- for(int i = 0; i < buttons.length; i++){
- buttons[i].addActionListener(buttonListener);
- }
- }
- //if overwrite is true, overwrite current input and then switch to false
- public static void overwriteCheck(int x){
- if(overwrite == true){
- input.setText("" + x);
- overwrite = false;
- }
- else
- input.append("" + x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment