如何使用信息自动填充 jRadioButtons?

How do I auto populate jRadioButtons with information?

我遇到了一个问题,但我不知道如何解决。在我下面的代码中,您可以看到我正在尝试创建一个 GUI,它使用提供的信息自动填充 20 个 jRadioButton,但是当我 运行 程序时,没有可见的按钮。有人可以指出我做错了什么吗? 包人;

    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;

    public class PersonInfoUI extends javax.swing.JFrame {

        public PersonInfoUI() {
            initComponents();
            ButtonGroup buttonGroup1 = new ButtonGroup();
            JRadioButton jRadioButton1 = new JRadioButton();
            personSelectorUI.add(jRadioButton1);
            personSelectorUI.revalidate();
            personSelectorUI.repaint();

        }
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {

            personSelectorUI = new javax.swing.JPanel();
            exitButton = new javax.swing.JButton();
            clearButton = new javax.swing.JButton();
            personInfoOutput = new javax.swing.JTextField();

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

            exitButton.setFont(new java.awt.Font("Times New Roman", 1, 18)); //          NOI18N
            exitButton.setText("Exit");
            exitButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    exitButtonActionPerformed(evt);
                }
            });

            clearButton.setFont(new java.awt.Font("Times New Roman", 1, 18)); //     NOI18N
            clearButton.setText("Clear");
            clearButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    clearButtonActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout personSelectorUILayout = new    
                javax.swing.GroupLayout(personSelectorUI);
            personSelectorUI.setLayout(personSelectorUILayout);
            personSelectorUILayout.setHorizontalGroup(

    personSelectorUILayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA    DING)
                .addGroup(personSelectorUILayout.createSequentialGroup()
                    .addContainerGap()

    .addGroup(personSelectorUILayout.createParallelGroup(javax.swing.GroupLayout.Ali    gnment.LEADING)
                        .addComponent(personInfoOutput,     
    javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,    
    personSelectorUILayout.createSequentialGroup()
                            .addComponent(exitButton,   
    javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)

    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(clearButton,  
    javax.swing.GroupLayout.PREFERRED_SIZE, 349, 
    javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
            );
            personSelectorUILayout.setVerticalGroup(

    personSelectorUILayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA    DING)
                .addGroup(personSelectorUILayout.createSequentialGroup()
                    .addGap(151, 151, 151)
                    .addComponent(personInfoOutput,   
    javax.swing.GroupLayout.PREFERRED_SIZE, 121,   
    javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(personSelectorUILayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(exitButton, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
                    .addComponent(clearButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(personSelectorUI, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(personSelectorUI, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>                        

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        personInfoOutput.setText("");
    }                                           

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        System.exit(0);
    }                                          

    public static void main(String args[]) {
        ButtonGroup buttonGroup1 = new ButtonGroup();

        List<Person> people = new ArrayList<>();
            people.add(new Person ("Nate Stoll", true, true, 1981));
            people.add(new Person ("Ashley Stoll", true, true, 1985));
            people.add(new Person ("Brooke Jackson", true, true, 1972));
            people.add(new Person ("Reed Stoll", true, true, 1983));
            people.add(new Person ("Reeda Stoll", true, true, 1942));
            people.add(new Person ("John Stoll", true, true, 1940));
            people.add(new Person ("Clark Kent", true, true, 1912));
            people.add(new Person ("Reed Richards", true, true, 1992));
            people.add(new Person ("Peter Parker", true, true, 1924));
            people.add(new Person ("Charles Xavier", true, true, 1905));
            people.add(new Person ("Bruce Banner", true, true, 1980));
            people.add(new Person ("Cheri Monaghan", true, true, 1979));
            people.add(new Person ("Matthew Groathouse", true, true, 1949));
            people.add(new Person ("John Williams", true, true, 1958));
            people.add(new Person ("Jake Holmes", true, true, 1998));
            people.add(new Person ("Bradley Cooper", true, true, 2015));
            people.add(new Person ("Shirley Temple", true, true, 1907));
            people.add(new Person ("Natalie Stoll", true, true, 1900));
            people.add(new Person ("Lindsay Gonzalez", true, true, 1970));
            people.add(new Person ("Tommy Chong", true, true, 1957));
    for(Person aPers : people){
        final JRadioButton jRadioButton1 = new JRadioButton(aPers.getName());
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (jRadioButton1.isSelected()){
                personInfoOutput.setText(jRadioButton1.getText()); }
                }
                });
                buttonGroup1.add(jRadioButton1);
        }
                new PersonInfoUI().setVisible(true);

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new PersonInfoUI().setVisible(true);
            }
         });
}


    // Variables declaration - do not modify                     
    private javax.swing.JButton clearButton;
    private javax.swing.JButton exitButton;
    private static javax.swing.JTextField personInfoOutput;
    private static javax.swing.JPanel personSelectorUI;
    // End of variables declaration                   

}

谢谢!

永远不要将新创建的 JRadioButton 添加到其祖先层次结构通向顶级 window 的任何容器中。换句话说,要在 GUI 中显示您的 JRadioButtons,需要添加到某些东西,通常是 JPanel,并且 JPanel 必须显示在您的 GUI 上——您没有这样做。我自己,我会创建一个使用 GridLayout 的 JPanel,也许是一个有 1 列和可变行的 JPanel,我会把我的 JRadioButtons 添加到这个,我会把 JPanel 添加到 JScrollPane,然后我会在我的某个地方添加 JScrollPane图形用户界面。我建议避免使用 GroupLayout 和 NetBeans 拖放式 GUI 创建来创建类似的东西,因为 GroupLayout 不喜欢您在 运行 时间添加新组件。我也不确定您为什么要在静态 main 方法中添加 JRadioButtons,这增加了它自己不必要的复杂性。如果您要对字符串进行硬编码,为什么不在构造函数中执行此操作?

要了解如何执行此操作,您需要阅读有关使用 JPanel 和布局管理器的教程。您可以在此处找到 Swing 教程和其他 Swing 资源的链接:Swing Info.

顺便说一句——看起来您真正想要使用的是一个 JTable,它在一列中有名称并且在一列中有一个复选框。