Example #1
0
  /** ** Constructor. Sets up user interface and initializations */
  public Secondtry() {
    operators = true;
    doClear = false;

    frame = new JFrame("Calculator");
    frame.setLayout(new BorderLayout(10, 10));

    display = new JTextArea();
    display.setSize(245, 100);
    display.setFont(new Font("SansSerif", Font.BOLD, 28));
    display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

    // create the numeric buttons for calculator
    for (int i = 0; i < 10; i++) {
      jb[i] = new JButton("" + (i));
      jb[i].addActionListener(this);
      jb[i].setFont(new Font("SansSerif", Font.BOLD, 14));
    }
    // create the square root button
    root = new JButton("\u221A");
    root.addActionListener(this);
    root.setFont(new Font("SansSerif", Font.BOLD, 14));

    // create the clear button
    clear = new JButton("C");
    clear.addActionListener(this);
    clear.setFont(new Font("SansSerif", Font.BOLD, 14));

    // create the plus button
    plus = new JButton("\u002B");
    plus.addActionListener(this);
    plus.setFont(new Font("SansSerif", Font.BOLD, 14));

    // create the minus button
    minus = new JButton("\u002D");
    minus.addActionListener(this);
    minus.setFont(new Font("SansSerif", Font.BOLD, 14));

    // create the multiply button
    mult = new JButton("\u002A");
    mult.addActionListener(this);
    mult.setFont(new Font("SansSerif", Font.BOLD, 14));

    // create the division button
    div = new JButton("\u002F");
    div.addActionListener(this);
    div.setFont(new Font("SansSerif", Font.BOLD, 14));

    // create the equals button
    equals = new JButton("\u003D");
    equals.addActionListener(this);
    equals.setFont(new Font("SansSerif", Font.BOLD, 14));

    equals.setPreferredSize(new Dimension(50, 60));

    // create the point button
    point = new JButton("\u002E");
    point.addActionListener(this);
    point.setFont(new Font("SansSerif", Font.BOLD, 14));

    // button grid
    JPanel buttons = new JPanel(new GridLayout(5, 4, 4, 4));

    // add buttons to grid
    buttons.add(clear);
    buttons.add(root);
    buttons.add(div);
    buttons.add(mult);
    // add numeric buttons to grid
    for (int i = 7; i < 10; i++) buttons.add(jb[i]);
    buttons.add(minus);
    // add numeric buttons to grid
    for (int i = 4; i < 7; i++) buttons.add(jb[i]);
    buttons.add(plus);
    // add numeric buttons to grid
    for (int i = 1; i < 4; i++) buttons.add(jb[i]);
    buttons.add(equals);
    buttons.add(jb[0]);
    buttons.add(point);

    frame.add(display, BorderLayout.CENTER);
    frame.add(buttons, BorderLayout.SOUTH);

    frame.setSize(300, 400);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }