Results 1 to 7 of 7
  1. #1
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default Calculator program

    I need help here! I'm trying to make a calculator program. Only the GUI with no functions, no listeners, no applets. AWT only. Here's the code:

    Java Code:
    import java.awt.*;
    
    public class Calculator
    {
    	private Frame frame;
    	private Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13,
    	b14, b15, b16, b17, b18;
    	private TextField display;
    	
    	public static void main(String args[])
    	{
    		Calculator calcu = new Calculator();
    		calcu.init();
    	}
    	public void init()
    	{
    		frame = new Frame("MKDL System Calculator");
    		frame.setSize(400,400);
    		frame.setBackground(Color.black);
    		display = new TextField("0.");
    		Panel panel = new Panel();
    		panel.setLayout (new GridLayout (3, 6));
    		b1 = new Button("7");
    		b2 = new Button("8");
    		b3 = new Button("9");
    		b4 = new Button("+");
    		b5 = new Button("");
    		b6 = new Button("=");
    		b7 = new Button("4");
    		b8 = new Button("5");
    		b9 = new Button("6");
    		b10 = new Button("-");
    		b11 = new Button("=");
    		b12 = new Button("Back");
    		b13 = new Button("1");
    		b14 = new Button("2");
    		b15 = new Button("3");
    		b16 = new Button("*");
    		b17 = new Button("+/-");
    		b18 = new Button("C");
    		panel.add(b1);
    		panel.add(b2);
    		panel.add(b3);
    		panel.add(b4);
    		panel.add(b5);
    		panel.add(b6);
    		panel.add(b7);
    		panel.add(b8);
    		panel.add(b9);
    		panel.add(b10);
    		panel.add(b11);
    		panel.add(b12);
    		panel.add(b13);
    		panel.add(b14);
    		panel.add(b15);
    		panel.add(b16);
    		panel.add(b17);
    		panel.add(b18);
    		panel.setSize (200,200);
    		panel.setBackground(Color.blue);
    		frame.add(display);
    		frame.add(panel);
    		frame.pack();
    		frame.setVisible(true);
    	}
    }
    The problem is - the text field isn't displayed, and then the frame size is limited to the buttons grid. It should be around 400 X 300. How can I fix that?

  2. #2
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    try this:

    display = new TextField("0.", 20);

    20 might be to big, but it should set the width of the textfield

  3. #3
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    And another problem: Why is the size of my output limited to the 18 buttons I put there? it should be 400 x 300 or something!

  4. #4
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default help!

    Here's the modified code:

    Java Code:
    import java.awt.*;
    
    public class Calculator
    {
    	private Frame frame;
    	private Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13,
    	b14, b15, b16, b17, b18;
    	private TextField display;
    	
    	public static void main(String args[])
    	{
    		Calculator calcu = new Calculator();
    		calcu.init();
    	}
    	public void init()
    	{
    		frame = new Frame("MKDL System Calculator");
    		frame.setSize(400,400);
    		frame.setBackground(Color.black);
    		display = new TextField("0.", 20);
    		frame.add(display);
    		Panel panel = new Panel();
    		panel.setLayout (new GridLayout (3,6));
    		panel.setSize(200,200);
    		b1 = new Button("7");
    		b2 = new Button("8");
    		b3 = new Button("9");
    		b4 = new Button("+");
    		b5 = new Button("");
    		b6 = new Button("=");
    		b7 = new Button("4");
    		b8 = new Button("5");
    		b9 = new Button("6");
    		b10 = new Button("-");
    		b11 = new Button("=");
    		b12 = new Button("Back");
    		b13 = new Button("1");
    		b14 = new Button("2");
    		b15 = new Button("3");
    		b16 = new Button("*");
    		b17 = new Button("+/-");
    		b18 = new Button("C");
    		panel.add(b1);
    		panel.add(b2);
    		panel.add(b3);
    		panel.add(b4);
    		panel.add(b5);
    		panel.add(b6);
    		panel.add(b7);
    		panel.add(b8);
    		panel.add(b9);
    		panel.add(b10);
    		panel.add(b11);
    		panel.add(b12);
    		panel.add(b13);
    		panel.add(b14);
    		panel.add(b15);
    		panel.add(b16);
    		panel.add(b17);
    		panel.add(b18);
    		panel.setSize (200,200);
    		panel.setBackground(Color.blue);
    		frame.add(panel);
    		frame.pack();
    		frame.setVisible(true);
    	}
    }
    I want the buttons on one panel, and the display text field above it.

  5. #5
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Update: here's the new code:

    Java Code:
    import java.awt.*;
    
    public class Calculator
    {
    	private Frame frame;
    	private Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13,
    	b14, b15, b16, b17, b18;
    	private TextField display;
    	
    	public static void main(String args[])
    	{
    		Calculator calcu = new Calculator();
    		calcu.init();
    	}
    	public void init()
    	{
    		frame = new Frame("MKDL System Calculator");
    		frame.setSize(400,400);
    		frame.setBackground(Color.black);
    		Panel panel = new Panel();
    		frame.setLayout (new GridLayout (2,1));
    		display = new TextField("0.", 20);
    		panel.setLayout (new GridLayout (3,6));
    		panel.setSize(200,200);
    		b1 = new Button("7");
    		b2 = new Button("8");
    		b3 = new Button("9");
    		b4 = new Button("+");
    		b5 = new Button("");
    		b6 = new Button("=");
    		b7 = new Button("4");
    		b8 = new Button("5");
    		b9 = new Button("6");
    		b10 = new Button("-");
    		b11 = new Button("=");
    		b12 = new Button("Back");
    		b13 = new Button("1");
    		b14 = new Button("2");
    		b15 = new Button("3");
    		b16 = new Button("*");
    		b17 = new Button("+/-");
    		b18 = new Button("C");
    		panel.add(b1);
    		panel.add(b2);
    		panel.add(b3);
    		panel.add(b4);
    		panel.add(b5);
    		panel.add(b6);
    		panel.add(b7);
    		panel.add(b8);
    		panel.add(b9);
    		panel.add(b10);
    		panel.add(b11);
    		panel.add(b12);
    		panel.add(b13);
    		panel.add(b14);
    		panel.add(b15);
    		panel.add(b16);
    		panel.add(b17);
    		panel.add(b18);
    		frame.add(display);
    		frame.add(panel);
    		frame.pack();
    		frame.setVisible(true);
    	}
    }
    How can I add some space?

  6. #6
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    Hello,

    to add some space you would need yo use a layout for your buttons on the panel. You dont have to use so much panels, by using one button per panel. Just set the layout of the JPanel then add all the buttons.

    e.g.
    Java Code:
    JPanel cont = new JPanel()
    cont.setLayout(new FlowLayout())
    :)

    GridBagLayout is Better in Looks but I forgot how to use it:( Is there anyone to tell me the parameters for the GridBagLayout.????

    :)

  7. #7
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    You can probably add Insets to your objects, that will add space around your objects. I haven't tried that in GridLayout, but it definatly works in GridBagLayout.

    You would have to import java.awt.GridBagLayout, GridBagConstraints, and Insets.

    you create a new GridBagConstraints object and set its parameters, then add the object to the container with add(object, constraints);

    Its a much more complex layout manager but once you get the hang of it, it by far the most functional.

Similar Threads

  1. Calculator Program HELP NEEDED FAST! Homework assignment
    By SteroidalPsycho in forum New To Java
    Replies: 3
    Last Post: 03-05-2009, 05:02 AM
  2. unreachable statement - Java calculator program
    By V2001Gordon in forum New To Java
    Replies: 3
    Last Post: 12-13-2008, 01:57 AM
  3. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 08:42 AM
  4. calculator not working
    By Renegade85 in forum New To Java
    Replies: 5
    Last Post: 03-10-2008, 04:27 PM

Posting Permissions

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