Java Reference
In-Depth Information
frame.setVisible(true);
}
private static JPanel makePanel(Color color) {
JPanel panel = new JPanel();
panel.setBackground(color);
return panel;
}
}
3.
Run the project from Eclipse. You should see the window in Figure 11-8.
figure 11-8  
How It Works
This is how it works:
1.
In this class, a JFrame is created and given a title like before, but its layout manager is changed to
be a FlowLayout. Then seven panels are added and colored with a rainbow pattern.
2.
Try resizing the window. Note how you cannot make the window smaller than its starting size.
This is due to the fact that this is the minimum size for windows in Windows, and thus the layout
manager has determined this to be the optimal size and has sized the panels accordingly as well,
since none of them contain any components. Now say you would like to make your rainbow a bit
bigger. You can do so by adding the following line to the makePanel method:
panel.setPreferredSize(new Dimension(100, 100));
3.
Run the program again; you will now see a bigger rainbow. Note that resizing the window shows
the FlowLayout in action, as shown in Figure 11-9.
figure 11-9  
Search WWH ::




Custom Search