Advertisement
gagan93

Simple Calculator

Jul 17th, 2014
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.GridLayout;
  3. import java.awt.BorderLayout;
  4.  
  5. // Declaring all calculator’s components.
  6. public class Calculator
  7. {
  8.     JPanel windowContent;
  9.     JTextField displayField;
  10.     JButton button0;
  11.     JButton button1;
  12.     JButton button2;
  13.     JButton button3;
  14.     JButton button4;
  15.     JButton button5;
  16.     JButton button6;
  17.     JButton button7;
  18.     JButton button8;
  19.     JButton button9;
  20.     JButton buttonPoint;
  21.     JButton buttonEqual;
  22.     JPanel p1;
  23.  
  24.     Calculator()
  25.     {
  26.         windowContent = new JPanel();
  27.         // Setting the layout manager for the panel.
  28.         BorderLayout b1 = new BorderLayout();
  29.         windowContent.setLayout(b1);
  30.         // Creating the display field.
  31.         displayField = new JTextField(30);
  32.         windowContent.add("North", displayField);
  33.         // Creating the buttons
  34.         button0 = new JButton("0");
  35.         button1 = new JButton("1");
  36.         button2 = new JButton("2");
  37.         button3 = new JButton("3");
  38.         button4 = new JButton("4");
  39.         button5 = new JButton("5");
  40.         button6 = new JButton("6");
  41.         button7 = new JButton("7");
  42.         button8 = new JButton("8");
  43.         button9 = new JButton("9");
  44.         buttonPoint = new JButton(".");
  45.         buttonEqual = new JButton("=");
  46.         // Create the panel with the GridLayout with 12 buttons
  47.         // 10 numeric ones, period, and the equal sign
  48.         p1 = new JPanel();
  49.         GridLayout g1 = new GridLayout(4, 3);
  50.         p1.setLayout(g1);
  51.         p1.add(button1);
  52.         p1.add(button2);
  53.         p1.add(button3);
  54.         p1.add(button4);
  55.         p1.add(button5);
  56.         p1.add(button6);
  57.         p1.add(button7);
  58.         p1.add(button8);
  59.         p1.add(button9);
  60.         p1.add(button0);
  61.         p1.add(buttonPoint);
  62.         p1.add(buttonEqual);
  63.         // Adding the panel p1 to the center of the window
  64.         windowContent.add("Center", p1);
  65.         // Creating the frame and set its content pane
  66.         JFrame frame = new JFrame("Calculator");
  67.         frame.setContentPane(windowContent);
  68.         // Setting the size of the window big enough to accomodate
  69.         frame.pack();
  70.         // Display the window
  71.         frame.setVisible(true);
  72.     }
  73.  
  74.     public static void main(String[] args)
  75.     {
  76.         Calculator calc = new Calculator();
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement