0

我在将 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 表中获取数据并创建单选并将数据放入其中,因此,我可以将它们添加到 ButtonGroup 但不能添加到 JPanel。或者,如果我不将它们添加到组并将它们添加到 JPanel 我无法正常切换它们,它们(单选按钮)就像复选框一样工作。

4

3 回答 3

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);
    }
于 2014-03-15T18:14:11.673 回答
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);
            }
        });
    }
}

有关详细信息,请参阅如何使用 ButtonGroup 组件

于 2014-03-15T18:19:10.503 回答
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);
}
于 2014-03-15T18:29:20.857 回答