【发布时间】: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