将JRadioButton添加到组

时间:2014-03-15 18:06:05

标签: java swing jpanel jradiobutton buttongroup

我有一些问题,将JRadioButton添加到ButtonGroup然后再添加到JPanel,这里有一些代码:

void modelsRadio () throws SQLException {

    JPanel modelsRadioPanel = new JPanel();

    Statement statement = db.setConnection();
    ResultSet rs = statement.executeQuery("SELECT * FROM МОДЕЛИ");
    ButtonGroup modelRadioGroup = new ButtonGroup();

    while (rs.next()) {

        modelsRadioPanel.add(new JRadioButton(rs.getString("НАЗВАНИЕ")));
        //modelRadioGroup.add(new JRadioButton(rs.getString("НАЗВАНИЕ")));
    }

    frame.add(modelsRadioPanel);
}

我们的想法是从Oracle SQL Table获取数据并创建无线电并将数据添加到它们中,因此,我可以将它们添加到ButtonGroup但不能添加到JPanel。或者,如果我没有将它们添加到组中并将它们添加到JPanel中,我就无法在它们之间正常切换,它们(单选按钮)的工作方式就像一个复选框。

3 个答案:

答案 0 :(得分:2)

您需要将每个单选按钮添加到面板和按钮组:

void modelsRadio () throws SQLException {

        JPanel modelsRadioPanel = new JPanel();

        Statement statement = db.setConnection();
        ResultSet rs = statement.executeQuery("SELECT * FROM МОДЕЛИ");
        ButtonGroup modelRadioGroup = new ButtonGroup();

        while (rs.next()) {
           JRadioButton jRadioButton =new JRadioButton(rs.getString("НАЗВАНИЕ")));

          //Add radio button to the panel
          modelsRadioPanel.add(jRadioButton);

          //Add radio button to the button group
          modelRadioGroup.add(jRadioButton);

          //Same for the remaining JRadioButton's 

        }

       // No need to add the button group to the panel

        frame.add(modelsRadioPanel);
    }

答案 1 :(得分:1)

在创建按钮时,将它们添加到ButtonGroup和面板。确保单选按钮的实例同时进入面板和按钮组。在代码中,您可以为面板创建一个实例,为该组创建一个实例。这是一个基本的例子:

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;

public class TestRadio {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame("TestRadio");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JPanel panel = new JPanel();
                ButtonGroup modelRadioGroup = new ButtonGroup();

                for (int i = 0; i < 5; i++) {
                    JRadioButton b1 = new JRadioButton("Radio" + i);

                    modelRadioGroup.add(b1);
                    panel.add(b1);
                }

                f.add(panel);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }
}

有关详细信息,请参阅How to Use the ButtonGroup Component

答案 2 :(得分:0)

嗯,我这样解决了:

void modelsRadio () throws SQLException {

    JPanel modelsRadioPanel = new JPanel();

    Statement statement = db.setConnection();
    ResultSet rs = statement.executeQuery("SELECT * FROM МОДЕЛИ");
    ButtonGroup modelRadioGroup = new ButtonGroup();

    while (rs.next()) {

       JRadioButton jr = new JRadioButton(rs.getString("НАЗВАНИЕ"));
       //modelRadioGroup.add(new JRadioButton(rs.getString("НАЗВАНИЕ")));
       modelRadioGroup.add(jr);
       modelsRadioPanel.add(jr);   
    }

    frame.add(modelsRadioPanel);
}