buttongroup的getselection

导读 大家好,小珊来为大家解答以上的问题。buttongroup的getselection这个很多人还不知道,现在让我们一起来看看吧!1、void    add(Abstract

大家好,小珊来为大家解答以上的问题。buttongroup的getselection这个很多人还不知道,现在让我们一起来看看吧!

1、void    add(AbstractButton b) :将按钮添加到组中。

2、     void    clearSelection() :清除选中内容,从而没有选择 ButtonGroup 中的任何按钮。

3、  int    getButtonCount() :返回此组中的按钮数。

4、     Enumeration getElements() :返回此组中的所有按钮。

5、     ButtonModel    getSelection() :返回选择按钮的模型。

6、     boolean    isSelected(ButtonModel m) :返回对是否已选择一个 ButtonModel 的判断。

7、     void    remove(AbstractButton b) :从组中移除按钮。

8、     void    setSelected(ButtonModel m, boolean b) :为 ButtonModel 设置选择值。

9、    Java中获得ButtonGroup选项的方法如下:public class Main { public static void main(String[] args) { JFrame frame = new JFrame(); //创建一个frame JPanel entreePanel = new JPanel(); final ButtonGroup entreeGroup = new ButtonGroup(); JRadioButton radioButton; entreePanel.add(radioButton = new JRadioButton("A")); radioButton.setActionCommand("A"); entreeGroup.add(radioButton); entreePanel.add(radioButton = new JRadioButton("B")); radioButton.setActionCommand("B"); entreeGroup.add(radioButton); entreePanel.add(radioButton = new JRadioButton("C", true)); radioButton.setActionCommand("C"); entreeGroup.add(radioButton); // 在entreeGroup中添加三个radio,分别为A B C final JPanel condimentsPanel = new JPanel(); condimentsPanel.add(new JCheckBox("Ketchup")); condimentsPanel.add(new JCheckBox("Mustard")); condimentsPanel.add(new JCheckBox("Pickles")); JPanel orderPanel = new JPanel(); JButton orderButton = new JButton("Place Order"); orderPanel.add(orderButton); frame.setLayout(new GridLayout(3, 1)); frame.add(entreePanel); frame.add(condimentsPanel); frame.add(orderPanel); //为每一个按钮添加一个监听事件: orderButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { String entree = entreeGroup.getSelection().getActionCommand(); System.out.println(entree + " sandwich"); Component[] components = condimentsPanel.getComponents(); for (Component c : components) { JCheckBox cb = (JCheckBox) c; if (cb.isSelected()) //这里判断是不是选中,如果是true就是选中,否则没有选中 System.out.println("With " + cb.getText()); } } }); //设置frame的位置 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 150); frame.setVisible(true); }}void add(AbstractButton b) 将按钮添加到组中。

10、 void clearSelection() 清除选中内容,从而没有选择 ButtonGroup 中的任何按钮。

11、 int getButtonCount() 返回此组中的按钮数。

12、 Enumeration getElements() 返回此组中的所有按钮。

13、 ButtonModel getSelection() 返回选择按钮的模型。

14、 boolean isSelected(ButtonModel m) 返回对是否已选择一个 ButtonModel 的判断。

15、 void remove(AbstractButton b) 从组中移除按钮。

16、 void setSelected(ButtonModel m, boolean b) 为 ButtonModel 设置选择值。

本文到此分享完毕,希望对大家有所帮助。