![](/img/trans.png)
[英]How can the mouse listener activate after a certain time when the mouse is already over the button?
[英]How to remove mouse listener after click on button?
這是非常具體的。 所以我正在做一個小的測驗應用程序,一切都完成了,但是我有問題。 因此,當我單擊正確答案時,它會變為綠色,並且correctAnswer計數器會上升。 如果我單擊錯誤的答案,它將變為紅色,而正確的答案將變為綠色。 我有下一個按鈕來回答下一個問題。
所以您已經知道問題出在哪里? 當我單擊答案時,我仍然可以單擊其他答案。 單擊后,我不想禁用按鈕,因為我希望它們變為綠色/紅色。
我希望只能在選擇答案時單擊下一步按鈕。 但是我不想禁用答案按鈕,因為它們會失去顏色。
這是我的一些方法
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jButton1.setBackground(Color.green);
Kviz.correctAnswers++;
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jButton1.setBackground(Color.green);
jButton2.setBackground(Color.red);
// TODO add your handling code here:
}
只需創建一個名為answerPicked
的變量answerPicked
。 將其設置為false,然后在用戶選擇答案時將其設置為true。 如果已經選擇了答案,則使用它可以阻止操作偵聽器執行任何操作。 然后,當用戶移至下一個問題時,可以將answerPicked
重置為false,以便用戶可以再次選擇答案。
為什么不創建一個布爾實例變量來控制用戶是否單擊。
boolean bIsClicked = false;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(bIsClicked == false){
jButton1.setBackground(Color.green);
Kviz.correctAnswers++;
//change the status
bIsClicked = true;
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if(bIsClicked == false){
jButton1.setBackground(Color.green);
jButton2.setBackground(Color.red);
// TODO add your handling code here:
//change the status
bIsClicked = true;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.