Results 1 to 3 of 3

Thread: Calculator Code

  1. #1
    threlot is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Calculator Code

    I want to use flowLayout() method and the setpreferredSize() on my program.

    import java.awt.Dimension;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;

    public class Calculator1 extends JFrame{
    private JPanel buttons1;

    private JTextField calc;


    public Calculator1(){
    super("Calculator");
    Container container=getContentPane();

    buttons1=new JPanel();
    buttons1.setLayout(new GridLayout(4,4,2,2));

    calc=new JTextField();

    buttons1.add(new JButton("7"));
    buttons1.add(new JButton("8"));
    buttons1.add(new JButton("9"));
    buttons1.add(new JButton("/"));
    buttons1.add(new JButton("4"));
    buttons1.add(new JButton("5"));
    buttons1.add(new JButton("6"));
    buttons1.add(new JButton("*"));
    buttons1.add(new JButton("1"));
    buttons1.add(new JButton("2"));
    buttons1.add(new JButton("3"));
    buttons1.add(new JButton("-"));
    buttons1.add(new JButton("0"));
    buttons1.add(new JButton("."));
    buttons1.add(new JButton("="));
    buttons1.add(new JButton("+"));

    container.add(buttons1, BorderLayout.CENTER);
    container.add(calc, BorderLayout.NORTH);


    setSize(265, 235);
    setResizable(false);
    setVisible(true);
    }
    public static void main (String args[])
    {
    Calculator1 application = new Calculator1();
    application.setDefaultCloseOperation(JFrame.EXIT_O N_CLOSE);
    }
    }

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: Calculator Code

    what have you attempted? all i can see if a grid layout.

  3. #3
    threlot is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Calculator Code

    Quote Originally Posted by monkeyjr97 View Post
    what have you attempted? all i can see if a grid layout.
    I want to know if there's a way that i can change the BorderLayout for FlowLayout because the size of the boxes are not the same.


    This is my code now:

    import java.awt.Dimension;
    import java.awt.*;
    import javax.swing.*;

    public class Calculator1 extends JFrame
    {
    private JPanel buttons1;

    private JTextField calc;


    public Calculator1(){
    super("Calculator");
    Container container = getContentPane();

    buttons1 = new JPanel();
    buttons1.setLayout(new FlowLayout(FlowLayout.CENTER,8 ,10));

    calc = new JTextField();

    buttons1.add(new JButton("7"));
    buttons1.add(new JButton("8"));
    buttons1.add(new JButton("9"));
    buttons1.add(new JButton("/"));
    buttons1.add(new JButton("4"));
    buttons1.add(new JButton("5"));
    buttons1.add(new JButton("6"));
    buttons1.add(new JButton("*"));
    buttons1.add(new JButton("1"));
    buttons1.add(new JButton("2"));
    buttons1.add(new JButton("3"));
    buttons1.add(new JButton("-"));
    buttons1.add(new JButton("0"));
    buttons1.add(new JButton("."));
    buttons1.add(new JButton("="));
    buttons1.add(new JButton("+"));

    container.add(buttons1, BorderLayout.CENTER);
    container.add(calc, BorderLayout.NORTH);


    setPreferredSize(new Dimension(200, 200));
    pack();
    setResizable(false);
    setVisible(true);
    }

    public static void main (String args[])
    {
    Calculator1 application = new Calculator1();
    application.setDefaultCloseOperation(JFrame.EXIT_O N_CLOSE);
    }
    }

Similar Threads

  1. "Newbie needs help with code" Calculator
    By mattyd27 in forum NetBeans
    Replies: 2
    Last Post: 08-20-2013, 10:35 PM
  2. Replies: 5
    Last Post: 11-23-2012, 03:00 AM
  3. need help with my basic java calculator code
    By hashey100 in forum New To Java
    Replies: 3
    Last Post: 11-12-2011, 05:48 PM
  4. Basic Java Calculator Code Problem
    By SerbianSergeant in forum New To Java
    Replies: 7
    Last Post: 07-22-2011, 02:50 PM
  5. Helo with calculator GUI and code
    By ls7897 in forum New To Java
    Replies: 7
    Last Post: 03-13-2011, 08:03 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •