java - 更改 JRadioButtons 在 JPanel 上显示的方式

标签 java swing netbeans

我有一个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();
    }

我的布局是 JPanelFlowLayout .

这就是JRadioButtons的方式显示:

enter image description here

我要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/

相关文章:

java - 从 SCROLL_TAB_LAYOUT JTabbedPane 中删除选项卡后,将剩余选项卡滚动至可见

java - 动态报告 : Showing error in compilation with NetBeans

java - 如何将数字时间转换为英文名称

Java:Google+ OAuth header 无效

java - 使用 int[] 填充 JComboBox

java - 将默认窗口位置更改为第二台显示器?

java - 如何从 GitHub 存储库中删除不正确的路径名?

java - Netbeans 禁用水平滚动

java - 关闭 ExecutorService

java - 使用 Maven 模块化项目