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

时间:2015-03-29 14:17:07

标签: java swing netbeans jradiobutton

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

    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                   

}

谢谢!

1 个答案:

答案 0 :(得分:1)

您永远不会将新创建的JRadioButton添加到其祖先层次结构通向顶级窗口的任何容器中。换句话说,为了让你的JRadioButtons显示在GUI中,需要添加到某些东西,通常是JPanel,并且必须在你的GUI上显示JPanel - 你不会这样做。我自己,我创建了一个使用GridLayout的JPanel,也许是一个包含1列和可变行的JPanel,我将我的JRadioButtons添加到此,我将JPanel添加到JScrollPane并且我&#39 ; d将JScrollPane添加到我的GUI。我建议避免使用GroupLayout和NetBeans拖放GUI创建来创建这样的东西,因为GroupLayout不喜欢在运行时添加新组件。我也不确定你为什么要尝试在静态main方法中添加JRadioButton,这会增加其不必要的复杂性。如果您对字符串进行硬编码,为什么不在构造函数中执行此操作?

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

顺便说一下 - 看起来你真正想要使用的是一个JTable,一个列中有名字,列中有一个复选框。