0

I have to do a basic program of a calulator(text field at top for display and then all numbers) it does not have to be funtional. I know you can only have one layout manager per container...but I can't get it to work? This is what I have so far..any feedback would be great. Thanks

--------------------------------------------------------------------

// Calculator
// 

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

public class Calculator extends JFrame
{
	private JButton button[];
	private final String names[] = 
		{ "7", "8", "9", "/", "4", "5", "6", "*", "1", 
		"2", "3", "-", "0", ".", "=", "+"};

	private JTextField field;
	
	private GridLayout grid;
	private BorderLayout boarder;

	public Calculator()
	{
		super( "Calculator");
		
		boarder = new BorderLayout(5,1);

		Container container1 = getContentPane();
		container1.setLayout( boarder );

		field = new JTextField("",25);
		
		container1.add(field, BorderLayout.NORTH);

		grid = new GridLayout(4, 4, 5, 5);
		
		Container container = getContentPane();
		container.setLayout( grid );

		button = new JButton[names.length];

		for(int count=0; count < names.length; count++)
		{
			button[count] = new JButton(names[count]);
			container.add(button[count]);
		}

		setSize(400,200);
		setVisible( true );
	}

	public static void main( String args[] )
	{
		Calculator application = new Calculator();
      		application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	}
}

----------------------------------------------------------------

2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by ALT-F4
0

Give this a go...

// Calculator
// 

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

public class Calculator extends JFrame
{
    private JButton button[];
    private final String names[] = 
        { "7", "8", "9", "/", "4", "5", "6", "*", "1", 
        "2", "3", "-", "0", ".", "=", "+"};

    private JTextField field;

    private GridLayout grid;
    private BorderLayout boarder;

        private JPanel p1;

    public Calculator(String title)
    {
        //super(title);

        boarder = new BorderLayout(5,1);

        Container container1 = getContentPane();
        container1.setLayout( boarder );

        field = new JTextField("",25);

        container1.add(field, BorderLayout.NORTH);

        grid = new GridLayout(4,4,5,5);

                p1 = new JPanel();
        p1.setLayout( grid );
                container1.add(p1, BorderLayout.CENTER);

        button = new JButton[names.length];

        for(int count=0; count < names.length; count++)
        {
            button[count] = new JButton(names[count]);
            p1.add(button[count]);
        }

        setSize(300,400);
        setVisible( true );
    }

    public static void main( String args[] )
    {
        Calculator application = new Calculator("Calculator");
            application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }
} 

Easier not to make two containers. Just make one for the main window and fill that with panels instead.

Edited by pyTony: fixed formating

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.