简体   繁体   English

如何确保在 Java 中只选择了一个 JRadioButton?

[英]How Do I Make Sure Only One JRadioButton Is Selected In Java?

I have three JRadioButton s added to a JPanel .我在JPanel添加了三个JRadioButton However, when I select one, I can select another one and the previous one selected stays selected.但是,当我选择一个时,我可以选择另一个,而上一个选择的保持选中状态。 How can I make sure that only one is selected at a time?如何确保一次只选择一个?

My JPanel class, which is added by my main JFrame :我的JPanel类,由我的主JFrame添加:

public class MainPanel extends JPanel {

    private static JRadioButton addHouse, addRoad, calculateDist;

    static {
        addHouse = new JRadioButton("Add House");
        addRoad = new JRadioButton("Add Road");
        calculateDist = new JRadioButton("Calculate Distance");
    }

    MainPanel() {
        setBackground(Color.WHITE);

        addHouse.setBounds(50, 50, 100, 50);
        addRoad.setBounds(50, 150, 100, 50);
        addHouse.setBounds(50, 250, 100, 50);

        addHouse.setBackground(Color.WHITE);
        addRoad.setBackground(Color.WHITE);
        calculateDist.setBackground(Color.WHITE);

        add(addHouse);
        add(addRoad);
        add(calculateDist);
    }


}

As you didn't share the complete code i can't tell how to do it in your case but in general由于您没有分享完整的代码,我不知道如何在您的情况下做到这一点,但总的来说

It can be done like that可以这样做

ButtonGroup buttonGroup = new ButtonGroup() // create a button group , buttons in a button group knows how to behave together 
JRadioButton radioButton1 = new JRadioButton("R1"); // create your buttons 
JRadioButton radioButton2 = new JRadioButton("R2"); // as many you want

buttonGroup.add(radioButton1); // make them part of group
buttonGroup.add(radioButton2);
myJFrame.setLayout(new FlowLayout()); // or any layout you want
myJFrame.add(radioButton1);    // add buttons to jframe 
myJFrame.add(radioButton1); 

when buttons were added to JFrame (or any other container) they were part of a group , so group handles the communications between them, now you can check only one at a time, try commenting buttonGroup.add();当按钮被添加到 JFrame(或任何其他容器)时,它们是 group 的一部分,因此 group 处理它们之间的通信,现在您一次只能检查一个,尝试注释 buttonGroup.add(); you will loose that behaveiour .你会失去这种行为的。

Same thing can be achieved manually , it that case you will track all other radio buttons at every selection to check if others are already check and then uncheck them which is tedious so better use ButtonGroup class of swing同样的事情可以手动实现,在这种情况下,您将在每次选择时跟踪所有其他单选按钮以检查其他单选按钮是否已经选中,然后取消选中它们,这很乏味,因此最好使用 ButtonGroup 类的 Swing

You can add the JRadioButton s in a ButtonGroup instance first.您可以先在ButtonGroup实例中添加JRadioButton Here is a simple example:这是一个简单的例子:

// configure buttons bounds, background etc.

ButtonGroup buttonGroup= new ButtonGroup();
buttonGroup.add(addHouse);
buttonGroup.add(addRoad);
buttonGroup.add(calculateDist);

// add the buttons to the panel too.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对于 Java,如果我想做一个 if 语句以确保输入只包含一个字符,我应该如何进行? - For Java, if I want to do an if statement to make sure the input only takes in one character, how should I proceed? 如何确保仅启动一个线程 - How do I make sure only one thread gets started 如何检查“仅应选择一个jRadiobutton”的条件 - How to check condition that 'only one jRadiobutton should be selected' 如何使用ActionListener检测是否选择了一个JRadioButton? - How do I detect if a single JRadioButton is selected or not using ActionListener? 如何计算正确选择的JRadioButton并打印总的“分数”? - How do I count the correctly selected JRadioButton and Print a total “Score”? 如何从 ButtonGroup 中选择哪个 JRadioButton - How do I get which JRadioButton is selected from a ButtonGroup Java:如何更改JRadioButton的字体大小? - Java: How do I change the font size of a JRadioButton? 如何确保java中的Collections只包含一个Type? - How to make sure that Collections in java hold only one Type? 如何确保输入不是 null 并且只输入了一个字符? - How do I make sure that the input is not null and that only one character is being entered? 如何确保只能执行我的程序的一个实例? - How do I make sure only one instance of my program can be executed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM