简体   繁体   English

单击jbutton后激活jradiobutton的actionlistener

[英]activate actionlistener of jradiobutton after a jbutton is click

so i have jradiobuttons and their listeners contains like this: 所以我有jradiobuttons和他们的听众包含这样的:

if(VotePresidentPanel.rbPres1.isSelected()){
                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. 但是我希望我的jradiobuttons的侦听器仅在单击某个jbutton之后才起作用。 i tried putting the listeners of jradiobutton inside the listener of jbutton but it still doesn't work. 我尝试将jradiobutton的侦听器放入jbutton的侦听器中,但仍然无法正常工作。 here's the code of what i mentioned that i tried doing: 这是我提到的尝试执行的代码:

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
} 

please help thank you so much :) 请帮助非常感谢你:)


i have these jradiobuttons 我有这些jradiobuttons

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

i want them to be still clickable as it is a voting system, but my problem is every time i click on the radio buttons, the votes will increment even tho i still haven't clicked the VOTE button. 我想他们是仍然可点击,因为它是一个投票系统,但我的问题是我每次点击单选按钮时,该票将递增甚至尽管我还没有点击VOTE按钮。 i want the votes to increment only if after i clicked the VOTE button. 我想要的票仅增加后,如果我点击了VOTE按钮。

the listeners of jradiobuttons are like this/the functions of increment: jradiobuttons的侦听器如下所示/增量功能:

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

and i tried putting them inside the block of my VOTE listener, but still doesn't work. 我尝试将它们放在我的VOTE侦听器中,但仍然无法正常工作。 thank you for any help :) 感谢您的任何帮助 :)

EDIT 2 this is what i tried base from the reply of copeg 编辑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. 但是我希望我的jradiobuttons的侦听器仅在单击某个jbutton之后才起作用。

If you wish to have the JRadioButton still enabled, but not have it's ActionListener fire you can use a boolean flag that is evaluated in the JRadioButton ActionListener, initialize it to false and set to true in the JButton ActionListener 如果希望仍然启用JRadioButton,但不希望它触发ActionListener,则可以使用在JRadioButton ActionListener中评估的布尔标志,将其初始化为false并在JButton ActionListener中设置为true

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

If you don't mind if the JRadioButton is enabled, consider enabling/disabling the JRadioButton s only after the JButton is clicked - eg in an ActionListener added to the JButton . 如果您不介意是否启用了JRadioButton,请考虑仅在单击JButton之后(例如,在添加到JButtonActionListener才启用/禁用JRadioButton

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM