Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Calculator extends JFrame
- {
- private JTextField answerfield;
- private JButton one, two, three, four, five, six, seven, eight, nine, zero, add, sub, mul, div, equ;
- private String input1, input2, output;
- private double answer = 0.0;
- private boolean equalsClicked = false, opChosen = false;
- public static void createAndShowGUI()
- {
- // FRAME
- JFrame frame = new JFrame("Calculator");
- // NORTH SCREEN
- JPanel screen = new JPanel();
- screen.setPreferredSize(new Dimension(250, 60));
- frame.add(screen, BorderLayout.NORTH);
- // NORTH TEXT INPUT
- JTextField answerfield = new JTextField();
- answerfield.setPreferredSize(new Dimension(240, 50));
- screen.add(answerfield);
- frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- frame.pack();
- frame.setVisible(true);
- // SOUTH SCREEN
- JPanel buttons = new JPanel();
- buttons.setPreferredSize(new Dimension(250, 300));
- frame.add(buttons, BorderLayout.SOUTH);
- frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- frame.pack();
- frame.setVisible(true);
- JPanel button = new JPanel();
- button.setPreferredSize(new Dimension(230, 250));
- buttons.add(button);
- frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- frame.pack();
- frame.setVisible(true);
- // SOUTH BUTTONS
- button.setLayout(new GridLayout(4, 4, 8, 12));
- JButton seven = new JButton("7");
- button.add(seven);
- JButton eight = new JButton("8");
- button.add(eight);
- JButton nine = new JButton("9");
- button.add(nine);
- JButton div = new JButton("/");
- button.add(div);
- JButton four = new JButton("4");
- button.add(four);
- JButton five = new JButton("5");
- button.add(five);
- JButton six = new JButton("6");
- button.add(six);
- JButton mul = new JButton("*");
- button.add(mul);
- JButton one = new JButton("1");
- button.add(one);
- JButton two = new JButton("2");
- button.add(two);
- JButton three = new JButton("3");
- button.add(three);
- JButton sub = new JButton("-");
- button.add(sub);
- JButton zero = new JButton("0");
- button.add(zero);
- JButton dec = new JButton(".");
- button.add(dec);
- JButton equ = new JButton("=");
- button.add(equ);
- JButton addbutton = new JButton("+");
- button.add(addbutton);
- Numbers n = new Numbers();
- Calculations c = new Calculations();
- // ADD ACTION LISTENERS
- one.addActionListener(n);
- two.addActionListener(n);
- three.addActionListener(n);
- four.addActionListener(n);
- five.addActionListener(n);
- six.addActionListener(n);
- seven.addActionListener(n);
- eight.addActionListener(n);
- nine.addActionListener(n);
- zero.addActionListener(n);
- }
- // NUMBER'S ACTION LISTENERS
- private class Numbers implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- JButton src = (JButton) event.getSource();
- if(src.equals(one))
- {
- if(opChosen = false)
- {
- if(input1 == null)
- {
- input1 = "1";
- }
- else
- {
- input1 = input1 + "1";
- }
- }
- else
- {
- if(input2 == null)
- {
- input2 = "1";
- }
- else
- {
- input2 = input2 + "1";
- }
- }
- }
- if(src.equals(two))
- {
- if(opChosen = false)
- {
- if(input1 == null)
- {
- input1 = "2";
- }
- else
- {
- input1 = input1 + "2";
- }
- }
- else
- {
- if(input2 == null)
- {
- input2 = "2";
- }
- else
- {
- input2 = input2 + "2";
- }
- }
- }
- if(src.equals(three))
- {
- if(opChosen = false)
- {
- if(input1 == null)
- {
- input1 = "3";
- }
- else
- {
- input1 = input1 + "3";
- }
- }
- else
- {
- if(input2 == null)
- {
- input2 = "3";
- }
- else
- {
- input2 = input2 + "3";
- }
- }
- }
- if(src.equals(four))
- {
- if(opChosen = false)
- {
- if(input1 == null)
- {
- input1 = "4";
- }
- else
- {
- input1 = input1 + "4";
- }
- }
- else
- {
- if(input2 == null)
- {
- input2 = "4";
- }
- else
- {
- input2 = input2 + "4";
- }
- }
- }
- if(src.equals(five))
- {
- if(opChosen = false)
- {
- if(input1 == null)
- {
- input1 = "5";
- }
- else
- {
- input1 = input1 + "5";
- }
- }
- else
- {
- if(input2 == null)
- {
- input2 = "5";
- }
- else
- {
- input2 = input2 + "5";
- }
- }
- }
- if(src.equals(six))
- {
- if(opChosen = false)
- {
- if(input1 == null)
- {
- input1 = "6";
- }
- else
- {
- input1 = input1 + "6";
- }
- }
- else
- {
- if(input2 == null)
- {
- input2 = "6";
- }
- else
- {
- input2 = input2 + "6";
- }
- }
- }
- if(src.equals(seven))
- {
- if(opChosen = false)
- {
- if(input1 == null)
- {
- input1 = "7";
- }
- else
- {
- input1 = input1 + "7";
- }
- }
- else
- {
- if(input2 == null)
- {
- input2 = "7";
- }
- else
- {
- input2 = input2 + "7";
- }
- }
- }
- if(src.equals(eight))
- {
- if(opChosen = false)
- {
- if(input1 == null)
- {
- input1 = "8";
- }
- else
- {
- input1 = input1 + "8";
- }
- }
- else
- {
- if(input2 == null)
- {
- input2 = "8";
- }
- else
- {
- input2 = input2 + "8";
- }
- }
- }
- if(src.equals(nine))
- {
- if(opChosen = false)
- {
- if(input1 == null)
- {
- input1 = "9";
- }
- else
- {
- input1 = input1 + "9";
- }
- }
- else
- {
- if(input2 == null)
- {
- input2 = "9";
- }
- else
- {
- input2 = input2 + "9";
- }
- }
- }
- if(src.equals(zero))
- {
- if(opChosen = false)
- {
- if(input1 == null)
- {
- input1 = "0";
- }
- else
- {
- input1 = input1 + "0";
- }
- }
- else
- {
- if(input2 == null)
- {
- input2 = "0";
- }
- else
- {
- input2 = input2 + "0";
- }
- }
- }
- if(equalsClicked==false)
- {
- if(opChosen==false)
- {
- answerfield.setText(input1);
- }
- else
- {
- answerfield.setText(input2);
- }
- }
- }
- }
- // ADD CALCULATIONS'S ACTION LISTENERS
- private class Calculations implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- JButton src = (JButton) event.getSource();
- }
- }
- // MAIN FUNCTION
- public static void main(String[] args)
- {
- SwingUtilities.invokeLater(new Runnable()
- {
- public void run()
- {
- createAndShowGUI();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement