checkboxGroup有一个方法getSelectedCheckbox()能获得当前选中项,ButtonGroup似乎没有,如果不去访问它的AbstractButton项目,怎样才能知道当前选中项

解决方案 »

  1.   

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

  2.   

    Enumeration<AbstractButton> enume = bg.getElements();
    while(enume.hasMoreElements()) {
    AbstractButton ab = enume.nextElement();
    if (ab.isSelected()) {
    //......
    System.out.println(ab.getText());
    }
    }也可以直接按钮的监听事件: b1.addItemListener(new ItemListener() {

    public void itemStateChanged(ItemEvent e) {
    JRadioButton jb = (JRadioButton) e.getSource();
    if (jb.isSelected()) {
    //...... }
    }
    });
      

  3.   


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class MainFrame extends JFrame implements SwingConstants {
    private JRadioButton jopButton1 = new JRadioButton("中国");
    private JRadioButton jopButton2 = new JRadioButton("美国");
    private JRadioButton jopButton3 = new JRadioButton("英国");
    private JRadioButton jopButton4 = new JRadioButton("法国");
    private ButtonGroup buttonGroup = new ButtonGroup();
    private JPanel jPanel = new JPanel(); public void init() {
    buttonGroup.add(jopButton1);
    buttonGroup.add(jopButton2);
    buttonGroup.add(jopButton3);
    buttonGroup.add(jopButton4); jopButton1.addItemListener(new ItemListener() {
    public void itemStateChanged(ItemEvent e) {
    JRadioButton jop = (JRadioButton) e.getSource();
    if (jop.isSelected())
    JOptionPane.showMessageDialog(null, jop.getText()); }
    });
    jopButton2.addItemListener(new ItemListener() {
    public void itemStateChanged(ItemEvent e) {
    JRadioButton jop = (JRadioButton) e.getSource();
    if (jop.isSelected())
    JOptionPane.showMessageDialog(null, jop.getText());
    }
    });
    jopButton3.addItemListener(new ItemListener() {
    public void itemStateChanged(ItemEvent e) {
    JRadioButton jop = (JRadioButton) e.getSource();
    if (jop.isSelected())
    JOptionPane.showMessageDialog(null, jop.getText());
    } });
    jopButton4.addItemListener(new ItemListener() {
    public void itemStateChanged(ItemEvent e) {
    JRadioButton jop = (JRadioButton) e.getSource();
    if (jop.isSelected())
    JOptionPane.showMessageDialog(null, jop.getText());
    }
    }); jPanel.add(jopButton1);
    jPanel.add(jopButton2);
    jPanel.add(jopButton3);
    jPanel.add(jopButton4);
    this.add(jPanel);
    }

    public MainFrame() {
    init();
    this.setTitle("示例");
    this.setSize(800,600);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
    new MainFrame();
    }}给按钮添加监听事件的实例。
      

  4.   


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Enumeration;public class MainFrame extends JFrame implements ItemListener {
    private JRadioButton jopButton1 = new JRadioButton("中国");
    private JRadioButton jopButton2 = new JRadioButton("美国");
    private JRadioButton jopButton3 = new JRadioButton("英国");
    private JRadioButton jopButton4 = new JRadioButton("法国");
    private ButtonGroup buttonGroup = new ButtonGroup();
    private JPanel jPanel = new JPanel(); public void init() {
    buttonGroup.add(jopButton1);
    buttonGroup.add(jopButton2);
    buttonGroup.add(jopButton3);
    buttonGroup.add(jopButton4);

    jopButton1.addItemListener(this);
    jopButton2.addItemListener(this);
    jopButton3.addItemListener(this);
    jopButton4.addItemListener(this); jPanel.add(jopButton1);
    jPanel.add(jopButton2);
    jPanel.add(jopButton3);
    jPanel.add(jopButton4);
    this.add(jPanel);
    }

    public MainFrame() {
    init();
    this.setTitle("示例");
    this.setSize(800,600);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * 每次有按钮改变的时候,循环去检查是那个被选中了。
     */
    public void itemStateChanged(ItemEvent e) {
    Enumeration<AbstractButton> enume = buttonGroup.getElements();
    while(enume.hasMoreElements()) {
    AbstractButton button = enume.nextElement();
    if(button.isSelected()) {
    JOptionPane.showMessageDialog(null, button.getText());
    }
    }
    }

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

  5.   


    API文档上的:
    javax.swing 
    接口 ButtonModel
    所有超级接口: 
    ItemSelectable 
    所有已知实现类: 
    DefaultButtonModel, JToggleButton.ToggleButtonModel --------------------------------------------------------------------------------public interface ButtonModelextends ItemSelectable按钮的状态模型。 此模型用于常规按钮,也可以用于复选框和单选钮,它们是特殊种类的按钮。实际上,按钮的 UI 负责调用其模型上的方法来管理状态,细节如下: 简单地说,在一个常规按钮上按下并释放鼠标将触发按钮并导致触发 ActionEvent。可通过按钮(通常是空格键)的外观所定义的键盘键生成相同的行为。当按钮具有焦点时,按下并释放此键将得到相同的结果。对于复选框和单选钮,刚刚所描述的鼠标或键盘的等效序列导致选中该按钮。 详细地说,当与鼠标一起使用时,按钮的状态模型的工作方式如下: 
    在按钮的上方按下鼠标使得模型既被选中又被按下。只要鼠标保持按下,模型就保持按下,即使鼠标移到按钮之外也是如此。相反,只有当鼠标在按钮边界内保持按下时,模型才是被选中的(它可移进或移出按钮的边界,但是只有在按钮中的那部分时间模型才是被选中的)。当在模型被选中的同时释放鼠标时,也就是说当鼠标以前在按钮上按下(并且尚未释放),之后在该按钮上方释放时,将触发按钮和 ActionEvent。鼠标释放后,模型就处于未被选中和未被按下状态。 详细地说,当与键盘一起使用时,按钮状态模型的工作方式如下: 
    当按钮具有焦点时,按下键盘键所定义的外观使得模型既被选中又被按下。只要此键保持按下,模型就保持此状态。释放该键会将模型设置为未被选中和未被按下、触发按钮并导致触发 ActionEvent。 
      

  6.   

    buttonGroup.getSelection().getActionCommand();
      

  7.   

    可是JRadioButton并没有实现ButtonModel接口啊
      

  8.   

    按钮类 都从 AbstractButton 继承来的 getModel 方法。
      

  9.   

    先用JRadioButton的setActionCommand(String)方法设置命令字符串,就能用ButtonGroup.getSelection().getActionCommand()得到选中项了
      

  10.   

    actionCommand的默认值和 button.getText() 一致。
      

  11.   

    不对,actionCommand的默认值为null
      

  12.   

    scala> val button = new javax.swing.JRadioButton("Apple")
    button: javax.swing.JRadioButton = javax.swing.JRadioButton[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.synth.SynthBorder@e707bb,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=false,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Apple]scala> val ac = button.getActionCommand
    ac: java.lang.String = Apple