/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski
ISBN: 1-893115-78-X
Publisher: APress
*/
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BoxSample {
public static void main(String args[]) {
JButton button;
Vector buttons = new Vector();
Dimension dim;
JFrame frame = new JFrame("Box Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
JPanel topLeft = new JPanel();
topLeft.setLayout(new BoxLayout(topLeft, BoxLayout.X_AXIS));
topLeft.add(button = new JButton("One"));
buttons.add(button);
changeBoth(button);
topLeft.add(button = new JButton("Two"));
buttons.add(button);
changeBoth(button);
changeWidth(topLeft);
JPanel bottomLeft = new JPanel();
bottomLeft.setLayout(new BoxLayout(bottomLeft, BoxLayout.X_AXIS));
bottomLeft.add(button = new JButton("Six"));
buttons.add(button);
changeBoth(button);
bottomLeft.add(button = new JButton("Seven"));
buttons.add(button);
changeBoth(button);
changeWidth(bottomLeft);
JPanel left = new JPanel();
left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
left.add(topLeft);
left.add(button = new JButton("Four"));
buttons.add(button);
changeBoth(button);
left.add(bottomLeft);
changeBoth(left);
JPanel right = new JPanel();
right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
right.add(button = new JButton("Three"));
buttons.add(button);
changeWidth(button);
right.add(button = new JButton("Five"));
buttons.add(button);
changeBoth(button);
changeBoth(right);
contentPane.add(left);
contentPane.add(right);
tweak(buttons);
frame.pack();
frame.setVisible(true);
}
private static void changeWidth(JComponent comp) {
comp.setAlignmentX(Component.CENTER_ALIGNMENT);
comp.setAlignmentY(Component.CENTER_ALIGNMENT);
Dimension dim = comp.getPreferredSize();
dim.width = Integer.MAX_VALUE;
comp.setMaximumSize(dim);
}
private static void changeHeight(JComponent comp) {
comp.setAlignmentX(Component.CENTER_ALIGNMENT);
comp.setAlignmentY(Component.CENTER_ALIGNMENT);
Dimension dim = comp.getPreferredSize();
dim.height = Integer.MAX_VALUE;
comp.setMaximumSize(dim);
}
private static void changeBoth(JComponent comp) {
comp.setAlignmentX(Component.CENTER_ALIGNMENT);
comp.setAlignmentY(Component.CENTER_ALIGNMENT);
Dimension dim = new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
comp.setMaximumSize(dim);
}
private static void tweak(Vector buttons) {
// calc max preferred width
JButton button;
Dimension dim;
int maxWidth = 0;
Enumeration e = buttons.elements();
while (e.hasMoreElements()) {
button = (JButton) e.nextElement();
dim = button.getPreferredSize();
if (dim.width > maxWidth)
maxWidth = dim.width;
}
// set max preferred width
e = buttons.elements();
while (e.hasMoreElements()) {
button = (JButton) e.nextElement();
dim = button.getPreferredSize();
dim.width = maxWidth;
button.setPreferredSize(dim);
}
}
}
|