按钮单击时读取所选单选按钮

时间:2014-03-07 15:12:30

标签: java jbutton actionlistener jradiobutton

我试图在按钮点击时获取所选单选按钮的值。这是我设法做的,但结果是我得到了。

//宣布按钮

    JRadioButton choice1 = new JRadioButton("Choice 1");
    JRadioButton choice2 = new JRadioButton("Choice 2");
    JRadioButton choice3 = new JRadioButton("Choice 3");
    JRadioButton choice4 = new JRadioButton("Choice 4");

    choice1.setSelected(true);

//按钮分组

    ButtonGroup group = new ButtonGroup();
    group.add(choice1);
    group.add(choice2);
    group.add(choice3);
    group.add(choice4);

    JPanel radioPanel = new JPanel();
    radioPanel.add(choice1);
    radioPanel.add(choice2);
    radioPanel.add(choice3);
    radioPanel.add(choice4);

    JButton button = new JButton("Check Choice");
    button.addActionListener(new ClickListener(group));
    frame.add(button, BorderLayout.SOUTH);

//调用动作侦听器读取单选按钮

    static class ClickListener implements ActionListener{
    ButtonGroup group = new ButtonGroup();
    public ClickListener(ButtonGroup group){

        super();
        this.group = group;
    }

    public void actionPerformed(ActionEvent e){
        System.out.println(group.getSelection().getActionCommand());
    }
}

4 个答案:

答案 0 :(得分:1)

您永远不会设置JRadioButton的操作命令。所以,当你要求它时,你就会变空。

解决方案:

首先制作按钮的位置:

choice1.setActionCommand("1");
choice2.setActionCommand("2");
choice3.setActionCommand("3");
choice4.setActionCommand("4");

然后在您的actionPerformed方法中:

String cmd = group.getSelection().getActionCommand();
if(cmd.equalsIgnoreCase("1")) {
    // Button 1 Action
} else if(cmd.equalsIgnoreCase("2")) {
    // Button 2 Action
} else if(cmd.equalsIgnoreCase("3")) {
    // Button 3 Action
} else if(cmd.equalsIgnoreCase("4")) {
    // Button 4 Action
}

这允许您使用操作命令来区分按钮。这不是最干净的方式,但应该有效。

希望这有帮助!

答案 1 :(得分:0)

您的ActionListener可以查询您的四个JRadioButton实例,并使用JRadioButton的isSelected()方法检查选择了哪一个。

如果您希望在单击JRadioButton时发生事件并希望知道单击了JRadioButton,则可以向JRadioButton添加ActionListener,然后检查ActionEvent是否源自特定的JRadioButton,例如e.getSource ()== radioButton1。

答案 2 :(得分:0)

你应该让所有JRadioButton都是静态的,包括ButtonGroup。

然后你需要改变按钮监听器:

button.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(java.awt.event.ActionEvent arg0) {
            System.out.println(group.getSelection().getActionCommand());
        }           
    });

要修复NullPointerException,请将其用于所有的radiobuttons:

choice1.setActionCommand("Choice 1");

答案 3 :(得分:0)

这应该适合你:

public class Example extends JFrame implements ActionListener {

    private ButtonGroup group = null;
    private JRadioButton choice1 = new JRadioButton("Choice 1");
    private JRadioButton choice2 = new JRadioButton("Choice 2");
    private JRadioButton choice3 = new JRadioButton("Choice 3");
    private JRadioButton choice4 = new JRadioButton("Choice 4");

    public Example() {

        choice1.addActionListener(this);
        choice2.addActionListener(this);
        choice3.addActionListener(this);
        choice4.addActionListener(this);

        group = new ButtonGroup();
        group.add(choice1);
        group.add(choice2);
        group.add(choice3);
        group.add(choice4);

        JPanel radioPanel = new JPanel(new FlowLayout());
        radioPanel.add(choice1);
        radioPanel.add(choice2);
        radioPanel.add(choice3);
        radioPanel.add(choice4);

        add(radioPanel);

        setSize(400, 400);
        setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JRadioButton radio = null;
    if (e.getSource() instanceof JRadioButton) {
        radio = (JRadioButton) (e.getSource());
    }
        if (radio.equals(choice1)) {
            System.out.println(choice1.getText());
        }
    }