Jump to content

GridLayout Manager

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Deco

Deco

    Newbie

  • Members
  • PipPip
  • 28 posts
Can someone tell me where I have gone wrong with this code.

Whenever I run this all I will get is a screen showing one and a half rows of the buttons and then cutting off midway through the window. If I expand the window I will be able to see all the buttons but they will be confined to the very top of the screen and not be in the grid layout I specified.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;


class Keypad

{

	public void displayKeypad()

	{

		JFrame gridFrame = new JFrame("Keypad");

		gridFrame.setVisible(true);

		gridFrame.setSize(150,200);

		JPanel gridPanel = new JPanel();

		gridFrame.add(gridPanel);

		GridLayout grid = new GridLayout(4,3);

		GridFrame.setContentPane(grid);

		

		JButton button1 = new JButton("1");

		JButton button2 = new JButton("2");

		JButton button3 = new JButton("3");

		JButton button4 = new JButton("4");

		JButton button5 = new JButton("5");

		JButton button6 = new JButton("6");

		JButton button7 = new JButton("7");

		JButton button8 = new JButton("8");

		JButton button9 = new JButton("9");

		JButton button0 = new JButton("10");

		JButton buttonE = new JButton("Enter");

		JButton buttonC = new JButton("Clear");

		

		gridPanel.add(button1);

		gridPanel.add(button2);

		gridPanel.add(button3);

		gridPanel.add(button4);

		gridPanel.add(button5);

		gridPanel.add(button6);

		gridPanel.add(button7);

		gridPanel.add(button8);

		gridPanel.add(button9);

		gridPanel.add(buttonC);

		gridPanel.add(button0);

		gridPanel.add(buttonE);

	}

}


#2
tate

tate

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
just change the line "GridFrame.setContentPane(grid);" to "gridPanel.setLayout(grid);" and if the buttons don't pop up immediately move "gridFrame.setVisible(true);" to the bottom of the method.
twas brillig

#3
Deco

Deco

    Newbie

  • Members
  • PipPip
  • 28 posts
I spent the best part of 6hours trying to figure this out. I'm amazed it was this simple.
Thanks :)