簡體   English   中英

單擊按鈕上的“讀取選定的單選按鈕”

[英]Reading Selected Radio Button on button click

我試圖在單擊按鈕時獲取所選單選按鈕的值。 這是我設法做到的,但結果卻是空的。

//已聲明按鈕

    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());
    }
}

您永遠不會設置JRadioButton的操作命令。 因此,當您要求它時,您將得到null。

解:

首次創建按鈕的位置:

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
}

這使您可以使用動作命令來區分按鈕。 這不是最干凈的方法,但是應該可以。

希望這對您有所幫助!

您的ActionListener可以查詢您的四個JRadioButton實例,並使用JRadioButton的isSelected()方法檢查選擇了哪個實例。

如果您希望在單擊JRadioButton時發生事件並希望知道單擊了什么JRadioButton,則可以將ActionListener添加到JRadioButtons中,然后檢查ActionEvent是否源自特定的JRadioButton,例如e.getSource()= = radioButton1。

您應該使所有JRadioButton都是靜態的,包括ButtonGroup。

然后,您需要更改按鈕偵聽器:

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

要修復NullPointerException,請將其用於所有單選按鈕:

choice1.setActionCommand("Choice 1");

這應該為您工作:

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());
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM