【问题标题】:activate actionlistener of jradiobutton after a jbutton is click单击jbutton后激活jradiobutton的actionlistener
【发布时间】:2016-09-21 16:06:07
【问题描述】:

所以我有 jradiobuttons 和他们的听众包含这样的:

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

但我希望我的 jradiobuttons 的侦听器仅在我单击某个 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的监听器是这样的/增量的功能:

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

我尝试将它们放在我的 VOTE 侦听器的块中,但仍然无法正常工作。谢谢你的帮助:)

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

【问题讨论】:

  • i want the listeners of my jradiobuttons to only function AFTER i clicked a certain jbutto 禁用 JRadioButton,并在单击 JButton 时启用。 but it doesn't work 有助于充分解释doesn't work 的含义,也有助于发布minimal reproducible example
  • @copeg 我编辑了我的问题,希望现在可以理解了,呵呵。但是是的,我会尝试你的建议,如果它有效,我会告诉你非常感谢:)
  • @copeg 哦,它没有用。你能告诉我怎么做吗?我在我声明它们的地方做了setEnabled(false) 单选按钮,然后在if(e.getSource().equals(VoteButtonsPanel.btnVote)){} 块内做了setEnabled(true) 它们,但我不能再点击它们了。我将编辑我的问题以进一步澄清呵呵:)
  • but i can't click them anymore 这就是禁用的重点。让它们可点击不是您发布的要求的一部分
  • @copeg 我已经编辑了我的问题。嘿嘿嘿抱歉没有包括它嘿嘿嘿

标签: java swing user-interface jbutton jradiobutton


【解决方案1】:

但我希望我的 jradiobuttons 的侦听器仅在我单击某个 jbutton 之后才起作用。

如果您希望 JRadioButton 仍然启用,但不触发 ActionListener,您可以使用在 JRadioButton ActionListener 中评估的布尔标志,将其初始化为 false 并在 @ 中设置为 true 987654324@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 是否已启用,请考虑仅在单击 JButton 后启用/禁用 JRadioButtons - 例如在添加到 JButtonActionListener 中。

【讨论】:

  • 我认为我做错了:/或者我只是理解错了,但它不起作用。这是我尝试过的boolean enableJRadioButton = false; 然后是 JButton if(e.getSource().equals(VoteButtonsPanel.btnVote)){ enableJRadioButton=true; } 和我的 JRadioButton 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)); } }
  • 对您的原始问题进行编辑 - 以 cmets 格式发布的代码非常糟糕。
  • 布尔值在错误的范围内(例如方法的本地)...必须将它移到actionPerformed 方法之外。一个调试器(或者只是一些 println's(在这里对你来说非常有价值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
相关资源
最近更新 更多