Spr*_*ing 1 java swing alignment jpanel radio-button
我想我中心在radibuttons面板,但是当我居中根据按钮的文字lenght中心,所以是他们中心而不是相互对齐,并期待非常难看.我该如何修复我的代码?
radioButtonMenuPanel = new JPanel();
radioButtonMenuPanel.setLayout(new BoxLayout(radioButtonMenuPanel,
BoxLayout.PAGE_AXIS));
ButtonGroup group = new ButtonGroup();
for (String item : answerItems) {
JRadioButton radioButton = new JRadioButton(item);
radioButton.setAlignmentX(Component.Center_ALIGNMENT);
radioButtonMenuPanel.add(radioButton);
group.add(radioButton);
}
Run Code Online (Sandbox Code Playgroud)
你总是可以使用GridBagLayout.通过使用该布局,您可以将组件锚定在单元格任何一侧的网格中.
radioButtonMenuPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;
gbc.anchor = GridBagConstraints.WEST;
ButtonGroup group = new ButtonGroup();
for (String item : answerItems) {
JRadioButton radioButton = new JRadioButton(item);
group.add(radioButton);
radioButtonMenuPanel.add(radioButton, gbc);
}
Run Code Online (Sandbox Code Playgroud)
这样的事情应该有效.默认情况下GridBagLayout,屏幕中的组件以小网格为中心.在这种情况下,我们只是将单元格中的组件锚定到西方,其中组件将放置在第一列(gridx = 0)中,对于每个单选按钮,它将它定位在最后一个组件旁边.
希望有所帮助!
| 归档时间: |
|
| 查看次数: |
8697 次 |
| 最近记录: |