[英]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.