Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在NetBeans中取消选择属于按钮组的单选按钮_Java_User Interface_Netbeans_Netbeans 8 - Fatal编程技术网

Java 在NetBeans中取消选择属于按钮组的单选按钮

Java 在NetBeans中取消选择属于按钮组的单选按钮,java,user-interface,netbeans,netbeans-8,Java,User Interface,Netbeans,Netbeans 8,我正在制作一个用户界面,其中包括许多单选按钮和NetBeans中的按钮组。假设将两个单选按钮分配给同一按钮组。一旦你点击其中一个单选按钮,就不可能取消选择。如果用户再次单击readiobuttons,我该怎么做才能取消选择?以下是我的尝试: if (b1.isSelected()==true) { b1.setEnabled(false); } 但我不想让它失效。帮帮我 编辑:这里有一个便宜的方法: int k=0; private void mb1ActionPerformed(ja

我正在制作一个用户界面,其中包括许多单选按钮和NetBeans中的按钮组。假设将两个单选按钮分配给同一按钮组。一旦你点击其中一个单选按钮,就不可能取消选择。如果用户再次单击readiobuttons,我该怎么做才能取消选择?以下是我的尝试:

if (b1.isSelected()==true) {
   b1.setEnabled(false); 
}
但我不想让它失效。帮帮我

编辑:这里有一个便宜的方法:

int k=0;
private void mb1ActionPerformed(java.awt.event.ActionEvent evt) {                                    
if (mb1.isSelected()==true) { //mb1 is the radiobutton
    k++;
}
if (k%2==0) {
    buttonGroup1.clearSelection();
}

是的,但这会使UI变得混乱,有时必须点击按钮组中的单选按钮两次才能再次选择它们。

好的,这有点麻烦。有许多问题很难解决

ButtonGroup
是一个具体的类,因此定制版本很困难(这就是我喜欢
接口的原因),它更加复杂,因为它严重依赖
私有属性(或包私有属性),而这些属性没有提供任何合理的扩展点

因此,这就剩下了两种做法,要么使用反射,要么复制基类并进行我们自己的修改——这两种情况都不令人愉快

所以,在我复制的所有代码中,我只修改了一个方法

public void setSelected(ButtonModel m, boolean b) {
    if (b) {
        if (m != selection) {
            ButtonModel oldSelection = selection;
            selection = m;
            if (oldSelection != null && oldSelection != m) {
                oldSelection.setSelected(false);
            }
            m.setSelected(true);
        }
    } else if (selection != null) {
        ButtonModel oldSelection = selection;
        selection = null;
        oldSelection.setSelected(false);
    }
}
基本上,它现在支持取消选择当前元素

public class UnselectableButtonGroup extends ButtonGroup {

    // the list of buttons participating in this group
    protected Vector<AbstractButton> buttons = new Vector<AbstractButton>();

    /**
     * The current selection.
     */
    ButtonModel selection = null;

    /**
     * Creates a new <code>ButtonGroup</code>.
     */
    public UnselectableButtonGroup() {
    }

    /**
     * Adds the button to the group.
     *
     * @param b the button to be added
     */
    public void add(AbstractButton b) {
        if (b == null) {
            return;
        }
        buttons.addElement(b);

        if (b.isSelected()) {
            if (selection == null) {
                selection = b.getModel();
            } else {
                b.setSelected(false);
            }
        }

        b.getModel().setGroup(this);
    }

    /**
     * Removes the button from the group.
     *
     * @param b the button to be removed
     */
    public void remove(AbstractButton b) {
        if (b == null) {
            return;
        }
        buttons.removeElement(b);
        if (b.getModel() == selection) {
            selection = null;
        }
        b.getModel().setGroup(null);
    }

    /**
     * Clears the selection such that none of the buttons in the
     * <code>ButtonGroup</code> are selected.
     *
     * @since 1.6
     */
    public void clearSelection() {
        if (selection != null) {
            ButtonModel oldSelection = selection;
            selection = null;
            oldSelection.setSelected(false);
        }
    }

    /**
     * Returns all the buttons that are participating in this group.
     *
     * @return an <code>Enumeration</code> of the buttons in this group
     */
    public Enumeration<AbstractButton> getElements() {
        return buttons.elements();
    }

    /**
     * Returns the model of the selected button.
     *
     * @return the selected button model
     */
    public ButtonModel getSelection() {
        return selection;
    }

    /**
     * Sets the selected value for the <code>ButtonModel</code>. Only one
     * button in the group may be selected at a time.
     *
     * @param m the <code>ButtonModel</code>
     * @param b <code>true</code> if this button is to be selected,
     * otherwise <code>false</code>
     */
    public void setSelected(ButtonModel m, boolean b) {
        if (b) {
            if (m != selection) {
                ButtonModel oldSelection = selection;
                selection = m;
                if (oldSelection != null && oldSelection != m) {
                    oldSelection.setSelected(false);
                }
                m.setSelected(true);
            }
        } else if (selection != null) {
            ButtonModel oldSelection = selection;
            selection = null;
            oldSelection.setSelected(false);
        }
    }

    /**
     * Returns whether a <code>ButtonModel</code> is selected.
     *
     * @return <code>true</code> if the button is selected, otherwise
     * returns <code>false</code>
     */
    public boolean isSelected(ButtonModel m) {
        return (m == selection);
    }

    /**
     * Returns the number of buttons in the group.
     *
     * @return the button count
     * @since 1.3
     */
    public int getButtonCount() {
        if (buttons == null) {
            return 0;
        } else {
            return buttons.size();
        }
    }

}
我还研究了在按钮上使用
itemstener
changestener
(以及两者)来尝试和控制
ButtonModel

我也尝试过,但它对我不起作用(我没有花任何时间尝试调试它,但它可能对其他人仍然有效)

可运行的示例。。。
我想知道你是否可以使用
ButtonGroup#setSelected(ButtonModel,boolean)
可能的重复边注:
if(b1.isSelected()==true)
if(b1.isSelected())
是等价的,而后者在你这方面带来错误的风险更小。@mad程序员我试过了。如果我这样做的话,它会被取消选择,但是重新选择是不可能的。我的问题与此类似,但这是用C编写的,最重要的是使用了“.tag”,它在java或Netbeans中至少不起作用。这是一个很好的代码,我将尝试一下,并让您使用know@P.Pawar是啊,不高兴,但我已经到了这样一个地步,要么就是反射,如果可以的话,我喜欢避免使用反射:/我找到了一个解决方案,但它并不优雅。我会更新我的问题details@P.Pawar我对它唯一的问题可能是
ButtonGroup
是否独立于按钮进行更新,但它是否适合您的需要
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            UnselectableButtonGroup bg = new UnselectableButtonGroup();
            JRadioButton buttons[] = new JRadioButton[5];
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            for (int index = 0; index < buttons.length; index++) {
                buttons[index] = new JRadioButton("Button " + index);
                add(buttons[index], gbc);
                bg.add(buttons[index]);
            }
        }

    }

    public class UnselectableButtonGroup extends ButtonGroup {

        // the list of buttons participating in this group
        protected Vector<AbstractButton> buttons = new Vector<AbstractButton>();

        /**
         * The current selection.
         */
        ButtonModel selection = null;

        /**
         * Creates a new <code>ButtonGroup</code>.
         */
        public UnselectableButtonGroup() {
        }

        /**
         * Adds the button to the group.
         *
         * @param b the button to be added
         */
        public void add(AbstractButton b) {
            if (b == null) {
                return;
            }
            buttons.addElement(b);

            if (b.isSelected()) {
                if (selection == null) {
                    selection = b.getModel();
                } else {
                    b.setSelected(false);
                }
            }

            b.getModel().setGroup(this);
        }

        /**
         * Removes the button from the group.
         *
         * @param b the button to be removed
         */
        public void remove(AbstractButton b) {
            if (b == null) {
                return;
            }
            buttons.removeElement(b);
            if (b.getModel() == selection) {
                selection = null;
            }
            b.getModel().setGroup(null);
        }

        /**
         * Clears the selection such that none of the buttons in the
         * <code>ButtonGroup</code> are selected.
         *
         * @since 1.6
         */
        public void clearSelection() {
            if (selection != null) {
                ButtonModel oldSelection = selection;
                selection = null;
                oldSelection.setSelected(false);
            }
        }

        /**
         * Returns all the buttons that are participating in this group.
         *
         * @return an <code>Enumeration</code> of the buttons in this group
         */
        public Enumeration<AbstractButton> getElements() {
            return buttons.elements();
        }

        /**
         * Returns the model of the selected button.
         *
         * @return the selected button model
         */
        public ButtonModel getSelection() {
            return selection;
        }

        /**
         * Sets the selected value for the <code>ButtonModel</code>. Only one
         * button in the group may be selected at a time.
         *
         * @param m the <code>ButtonModel</code>
         * @param b <code>true</code> if this button is to be selected,
         * otherwise <code>false</code>
         */
        public void setSelected(ButtonModel m, boolean b) {
            if (b) {
                if (m != selection) {
                    ButtonModel oldSelection = selection;
                    selection = m;
                    if (oldSelection != null && oldSelection != m) {
                        oldSelection.setSelected(false);
                    }
                    m.setSelected(true);
                }
            } else if (selection != null) {
                ButtonModel oldSelection = selection;
                selection = null;
                oldSelection.setSelected(false);
            }
        }

        /**
         * Returns whether a <code>ButtonModel</code> is selected.
         *
         * @return <code>true</code> if the button is selected, otherwise
         * returns <code>false</code>
         */
        public boolean isSelected(ButtonModel m) {
            return (m == selection);
        }

        /**
         * Returns the number of buttons in the group.
         *
         * @return the button count
         * @since 1.3
         */
        public int getButtonCount() {
            if (buttons == null) {
                return 0;
            } else {
                return buttons.size();
            }
        }

    }

}