Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.GridLayout;
- import java.awt.BorderLayout;
- // Declaring all calculator’s components.
- public class Calculator
- {
- JPanel windowContent;
- JTextField displayField;
- JButton button0;
- JButton button1;
- JButton button2;
- JButton button3;
- JButton button4;
- JButton button5;
- JButton button6;
- JButton button7;
- JButton button8;
- JButton button9;
- JButton buttonPoint;
- JButton buttonEqual;
- JPanel p1;
- Calculator()
- {
- windowContent = new JPanel();
- // Setting the layout manager for the panel.
- BorderLayout b1 = new BorderLayout();
- windowContent.setLayout(b1);
- // Creating the display field.
- displayField = new JTextField(30);
- windowContent.add("North", displayField);
- // Creating the buttons
- button0 = new JButton("0");
- button1 = new JButton("1");
- button2 = new JButton("2");
- button3 = new JButton("3");
- button4 = new JButton("4");
- button5 = new JButton("5");
- button6 = new JButton("6");
- button7 = new JButton("7");
- button8 = new JButton("8");
- button9 = new JButton("9");
- buttonPoint = new JButton(".");
- buttonEqual = new JButton("=");
- // Create the panel with the GridLayout with 12 buttons
- // 10 numeric ones, period, and the equal sign
- p1 = new JPanel();
- GridLayout g1 = new GridLayout(4, 3);
- p1.setLayout(g1);
- p1.add(button1);
- p1.add(button2);
- p1.add(button3);
- p1.add(button4);
- p1.add(button5);
- p1.add(button6);
- p1.add(button7);
- p1.add(button8);
- p1.add(button9);
- p1.add(button0);
- p1.add(buttonPoint);
- p1.add(buttonEqual);
- // Adding the panel p1 to the center of the window
- windowContent.add("Center", p1);
- // Creating the frame and set its content pane
- JFrame frame = new JFrame("Calculator");
- frame.setContentPane(windowContent);
- // Setting the size of the window big enough to accomodate
- frame.pack();
- // Display the window
- frame.setVisible(true);
- }
- public static void main(String[] args)
- {
- Calculator calc = new Calculator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement