Java/Swing JFC/Alignment
Laying Out a Screen with Different Component Alignments
import java.awt.ruponent;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AlignY {
public static void main(String args[]) {
JFrame frame = new JFrame("Y Alignment");
JPanel contentPane = (JPanel) frame.getContentPane();
BoxLayout layout = new BoxLayout(contentPane, BoxLayout.X_AXIS);
contentPane.setLayout(layout);
JButton button = new JButton("0.0");
button.setAlignmentY(Component.TOP_ALIGNMENT);
contentPane.add(button);
button = new JButton("1.0");
button.setAlignmentY(Component.BOTTOM_ALIGNMENT);
contentPane.add(button);
button = new JButton("0.5");
button.setAlignmentY(Component.CENTER_ALIGNMENT);
contentPane.add(button);
frame.setSize(200, 100);
frame.show();
}
}
Working with Component Alignments
import java.awt.ruponent;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AlignX {
private static Container makeIt(String labelChar, float alignment) {
JPanel container = new JPanel();
BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
container.setLayout(layout);
for (int i = 1; i < 6; i++) {
String label = makeLabel(labelChar, i * 2);
JButton button = new JButton(label);
button.setAlignmentX(alignment);
container.add(button);
}
return container;
}
private static String makeLabel(String s, int length) {
StringBuffer buff = new StringBuffer(length);
for (int i = 0; i < length; i++) {
buff.append(s);
}
return buff.toString();
}
public static void main(String args[]) {
JFrame frame = new JFrame("X Alignment");
Container contentPane = frame.getContentPane();
Container panel1 = makeIt("R", Component.RIGHT_ALIGNMENT);
Container panel2 = makeIt("C", Component.CENTER_ALIGNMENT);
Container panel3 = makeIt("L", Component.LEFT_ALIGNMENT);
contentPane.setLayout(new FlowLayout());
contentPane.add(panel1);
contentPane.add(panel2);
contentPane.add(panel3);
frame.pack();
frame.show();
}
}