import java.awt.*;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
JComponent[] comps = {
new JCheckBox("CheckBox 1"), new JTextField(12),
new JCheckBox("CheckBox 2"), new JTextField(12)
};
JPanel jPanelVars = new JPanel(new BorderLayout());
JPanel jPanelBoEx = new JPanel();
jPanelBoEx.setBorder(BorderFactory.createEmptyBorder(0, 160, 0, 0));
jPanelVars.add(jPanelBoEx, BorderLayout.LINE_START);
jPanelBoEx.setLayout(new BoxLayout(jPanelBoEx, BoxLayout.PAGE_AXIS));
for(int j = 0; j < comps.length; j++) {
adjustAlignment(comps[j]);
if(j % 2 == 0) {
AbstractButton button = (AbstractButton)comps[j];
String text = button.getText();
button.setText("");
JLabel label = new JLabel(text);
//adjustAlignment(label);
Box box = Box.createHorizontalBox();
adjustAlignment(box);
box.add(button);
box.add(box.createHorizontalGlue());
box.add(label);
if(j == 2)
box.add(box.createHorizontalGlue());
jPanelBoEx.add(box);
} else {
jPanelBoEx.add(comps[j]);
}
}
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(jPanelVars); f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
private static void adjustAlignment(JComponent c) {
String name = c.getClass().getName();
name = name.substring(name.lastIndexOf(".")+1);
System.out.printf("%10s alignmentX = %.3f alignmentY = %.3f%n",
name, c.getAlignmentX(), c.getAlignmentY());
float left = Component.LEFT_ALIGNMENT;
float center = Component.CENTER_ALIGNMENT;
c.setAlignmentX(left);
}
}