单击 jbutton 后激活 jradiobutton 的 actionlistener

activate actionlistener of jradiobutton after a jbutton is click

所以我有 jradiobuttons,他们的听众包含这样的内容:

if(VotePresidentPanel.rbPres1.isSelected()){
                int[] incrementVote={1};
                for (int v : incrementVote){
                    resultP1 = (pvotes[v - 1]+=1);
                }
                ResultPresidentPanel.lblResPres1.setText(Integer.toString(resultP1));
            }

但我希望我的 jradiobutton 的侦听器仅在我单击某个 jbutton 后起作用。我尝试将 jradiobutton 的侦听器放在 jbutton 的侦听器中,但它仍然不起作用。这是我提到的我尝试做的代码:

if(e.getSource().equals(VoteButtonsPanel.btnVote)){
            if(VotePresidentPanel.rbPres1.isSelected()){
                int[] incrementVote={1};
                for (int v : incrementVote){
                    resultP1 = (pvotes[v - 1]+=1);
                }
                ResultPresidentPanel.lblResPres1.setText(Integer.toString(resultP1));
            }
            //...and other listeners for the other jradiobuttons
} 

请帮忙,非常感谢:)


我有这些 jradiobuttons

() Pres1
() Pres2
() Pres3
() Pres4

我希望它们仍然可以点击,因为它是一个投票系统,但我的问题是每次我点击单选按钮,即使我还没有点击 VOTE,投票也会增加按钮。我希望只有在单击 VOTE 按钮后投票才会增加。

jradiobuttons 的侦听器类似于 this/the 增量函数:

if(VotePresidentPanel.rbPres1.isSelected()){
                    int[] incrementVote={1};
                    for (int v : incrementVote){
                        resultP1 = (pvotes[v - 1]+=1);
                    }
                    ResultPresidentPanel.lblResPres1.setText(Integer.toString(resultP1));
                }

我尝试将它们放入我的 VOTE 侦听器的块中,但仍然不起作用。感谢您的帮助:)

编辑 2 这是我根据 copeg

的回复尝试的
        boolean enableJRadioButton = false;
        if(e.getSource().equals(VoteButtonsPanel.btnVote)){
            enableJRadioButton=true;
        }
        if(VotePresidentPanel.rbPres1.isSelected()){
            if(enableJRadioButton==true){
                int[] incrementVote={1};
                for (int v : incrementVote){
                    resultP1 = (pvotes[v - 1]+=1);
                }
                ResultPresidentPanel.lblResPres1.setText(Integer.toString(resultP1));
            }
        }

but i want the listeners of my jradiobuttons to only function AFTER i clicked a certain jbutton.

如果您希望 JRadioButton 仍然启用,但不激活它的 ActionListener,您可以使用在 JRadioButton ActionListener 中评估的布尔标志,将其初始化为 false 并设置为trueJButton ActionListener

boolean enableJRadioButton = false;
...
final JRadioButton myRadioButton = new JRadioButton("Do Something");
myRadioButton.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e){
        if ( enableJRadioButton ){
            //do something
        }
    }
});

myButton.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e){
        enableJRadioButton = true;
        //do something else if necessary
    }
});

如果您不介意 JRadioButton 是否已启用,请考虑 enabling/disabling JRadioButton 仅在 JButton 被单击后 - 例如在 ActionListener 中添加到 JButton