Just playing around with Swing and wondering why the following code constructs a layout that seems to have three columns when the GridLayout is defined as 10 rows and 10 columns?
Can anybody explain this unusual behaviour and what in the code provided is causing this to happen?
public class MyGrid {
public static void main (String[] args) {
JFrame frame = new JFrame();
Container container = frame.getContentPane();
container.setLayout(new GridLayout(10,10));
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i>=j) {
container.add(new JButton("X"));
} else {
container.add(new JLabel("Y"));
}
}
}
frame.setSize(500,500);
frame.setVisible(true);
}
}
See the class javadoc of
GridLayout:If you use this code
you would see 10 rows and 10 columns. If you use
i < 50for example in the for loop, the number of columns change.