Java Reference
In-Depth Information
Figure 10-9. Three y-axis BoxLayout containers, each with components having the same
horizontal alignments
The key point demonstrated here is that if all the components share the same alignment
setting, the actual alignment of all the components within the managed container is the
components' alignment setting.
The source used to generate Figure 10-9 is shown in Listing 10-2.
Listing 10-2. Y-Axis Alignment
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class YAxisAlignX {
private static Container makeIt(String title, float alignment) {
String labels[] = {"--", "----", "--------", "------------"};
JPanel container = new JPanel();
container.setBorder(BorderFactory.createTitledBorder(title));
BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
container.setLayout(layout);
for (int i=0,n=labels.length; i<n; i++) {
JButton button = new JButton(labels[i]);
button.setAlignmentX(alignment);
container.add(button);
}
return container;
}
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("Alignment Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container panel1 = makeIt("Left", Component.LEFT_ALIGNMENT);
Container panel2 = makeIt("Center", Component.CENTER_ALIGNMENT);
Container panel3 = makeIt("Right", Component.RIGHT_ALIGNMENT);
 
Search WWH ::




Custom Search