我需要对齐 GUI 中的复选框,但找不到正确的命令或方法。我写了一个更容易阅读的简短示例:
public class GUI {
JFrame window = new JFrame();
JPanel mainPanel = new JPanel();
JPanel[] rowPanel = new JPanel[5];
JCheckBox[] check = new JCheckBox[5];
public GUI () {
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setExtendedState(window.MAXIMIZED_BOTH);
window.setVisible(true);
window.add(mainPanel);
mainPanel.setLayout(new GridLayout(5, 1));
for(int i = 0; i < 5; i++)
{
rowPanel[i] = new JPanel();
mainPanel.add(rowPanel[i]);
}
check[0] = new JCheckBox("red");
check[1] = new JCheckBox("violet");
check[2] = new JCheckBox("pink");
check[3] = new JCheckBox("magenta");
check[4] = new JCheckBox("every color");
for(int i = 0; i < 5; i++)
{
rowPanel[i].add(check[i]);
}
}
}
我试过.setHorizontalTextPosition()
and .setHorizontalAlignment()
,但都没有奏效。我希望所有的框都在标签的右侧垂直对齐。