Reputation: 41
public void init_numSolvers() {
for (x = 0; x < 9; x++) {
n++;
num[x] = new Button("" + n);
add(num[x]);
num[x].setBounds(num_x, num_y, 40, 40);
setLayout(null);
num_x += 40;
}
why setBounds() function doesn't work without setLayout(null) i just want to understand the main purpose of setLayout(null) function
Upvotes: 1
Views: 27327
Reputation: 285405
You will want to read the tutorials and the API as they clearly spell out what setBounds and setLayout does, but briefly:
setLayout(...)
method allows you to set the layout of the container, often a JPanel, to say FlowLayout, BorderLayout, GridLayout, null layout, or whatever layout desired. The layout manager helps lay out the components held by this container.setBounds(...)
method is used to set the location and size of a single component, and is only useful if null
layout is used by the container that holds this component.null
layouts and setBounds(...)
. While to a newbie Swing programmer, this may seem the easiest way to create complex layouts, when using them you create GUI's that might look good on one platform only. More importantly, they create GUI's that are nearly impossible to maintain and upgrade without causing subtle bugs. Just don't use them if at all possible.Note that your code repeatedly sets the container's layout to null within the for loop. I have no idea why it does this repeatedly since you only need to and want to set a container's layout once.
Upvotes: 6