answersLogoWhite

0

You don't actually add items to a LayoutManager object. You should set the layout of the Container you want to add your Components to and the GridLayout will put them where they need to be.

Container myContainer = new Container();

int numRows = 3;

int numCols = 2;

GridLayout myLayoutManager = new GridLayout(numRows, numCols);

myContainer.setLayout(myLayoutManager);

// Add some JButtons to the container...

myContainer.add(new JButtons ("1"));

myContainer.add(new JButtons ("2"));

myContainer.add(new JButtons ("3"));

myContainer.add(new JButtons ("4"));

myContainer.add(new JButtons ("5"));

myContainer.add(new JButtons ("6"));

Your final component will look something like the following:

| 1 | 2 |

| 3 | 4 |

| 5 | 6 |

User Avatar

Wiki User

15y ago

Still curious? Ask our experts.

Chat with our AI personalities

TaigaTaiga
Every great hero faces trials, and you—yes, YOU—are no exception!
Chat with Taiga
CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
ReneRene
Change my mind. I dare you.
Chat with Rene

Add your answer:

Earn +20 pts
Q: How do you add items in GridLayout in Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp