Java Reference
In-Depth Information
Listing 8-2. A FlowLayout with Three Centered Buttons
import java.awt.*;
import javax.swing.*;
public class ExampleFlowLayout extends JFrame {
public static void main(String[] args) {
new ExampleFlowLayout().setVisible(true);
}
public ExampleFlowLayout () {
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel thePanel = new JPanel();
thePanel.add(new JButton("One"));
thePanel.add(new JButton("Two"));
thePanel.add(new JButton("Three"));
add(thePanel, BorderLayout.CENTER);
pack();
}
}
Figure 8-7. FlowLayout showing the default operation—all components laid out at their
preferred size next to each other
Figure 8-8. An example of FlowLayout when the container has been expanded
Figure 8-9. An example of FlowLayout when the container has been reduced in size
Notice that the button sizes are calculated by the FlowLayout and displayed at just the
right size to show their text labels. Also note that the default justification for the FlowLayout is
CENTER . Listing 8-3 demonstrates an alteration to the code segment in Listing 8-2 that changes
the justification of the buttons. An example of how this might appear is shown in Figure 8-10.
Search WWH ::




Custom Search