我有一个JPanel
,我想添加 JRadioButtons
对此,这是我尝试过的代码:
private void populateQuestionnaire(Question question){
buttonGroup = new ButtonGroup();
for(Choix c : question.getListChoix()) {
radioButton = new JRadioButton(c.getChoixLibelle());
buttonGroup.add(radioButton);
jPanel1.add(radioButton);
}
jPanel1.revalidate();
jPanel1.repaint();
}
我的布局是 JPanel
是 FlowLayout
.
这就是JRadioButtons
的方式显示:
我要JRadioButtons
将一个添加到另一个下面并在 JPanel 中居中。
最佳答案
您可以使用BoxLayout
,而不是使用从左到右布置项目并适当换行的FlowLayout
。 ,它允许您指定水平或垂直布局项目。
您可以在构造时为 JPanel
设置 LayoutManager
:
JPanel jpanel1 = new JPanel(new BoxLayout(parentComponent, BoxLayout.Y_AXIS));
关于java - 更改 JRadioButtons 在 JPanel 上显示的方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20939115/