[英]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));
}
但是我希望我的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
侦听器中,但仍然无法正常工作。 感谢您的任何帮助 :)
编辑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));
}
}
但是我希望我的jradiobuttons的侦听器仅在单击某个jbutton之后才起作用。
如果希望仍然启用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
}
});
如果您不介意是否启用了JRadioButton,请考虑仅在单击JButton
之后(例如,在添加到JButton
的ActionListener
才启用/禁用JRadioButton
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.