DSlomer64
DSlomer64

Reputation: 4283

mixing gridlayout and borderlayout

I was tired but before quitting just stuck in the nearly-last 3 lines in the code snippet below to make a "refresh" button on my tictactoe panel, hoping to get away with it but expecting errors, since it mixes layout managers on a single container.

But it WORKED.

ButtonPanel.setLayout(new GridLayout(3, 3));    
guiFrame.add(ButtonPanel); 
for (int i = 0; i < 3; i++) {
  for (int j = 0; j < 3; j++) {
    button[i][j] = addButton(ButtonPanel, i, j);
  }
}

JButton refreshbutton = new JButton("Refresh");
guiFrame.add(refreshbutton, BorderLayout.SOUTH); // ... border layout worked. Hm.
refreshbutton.addActionListener(this);

guiFrame.setVisible(true);   }

Should I be surprised? (Keep in mind my newbieness.)

(BOY, did I learn/stumble onto a buncha stuff in writing this silly game's program!!!--for instance, using setActionCommand to "label" each button internally [as 11,12,13,21,...33] so the ONE actionPerformed method could use getActionCommand to correctly label [with X or O] whatever button was pushed by whoever's turn it was.)

Upvotes: 0

Views: 1789

Answers (1)

camickr
camickr

Reputation: 324118

guiFrame.add(refreshbutton, BorderLayout.SOUTH); // ... border layout worked. Hm.

Just because you used BorderLayout.SOUTH does not make a panel a BorderLayout. Your code worked because the default layout manager for the content pane of a JFrame (JDialog) is a BorderLayout. So you are just taking advantage of the default layout.

since it mixes layout managers on a single container.

Yes, this is a common practice. In fact it is almost impossible to create a reasonably complex GUI if you don't use different layout managers on different panels that you add to a GUI.

Upvotes: 2

Related Questions