如何在单击按钮后删除鼠标侦听器?

时间:2016-04-16 18:36:39

标签: java swing button netbeans

这是非常具体的。所以我正在制作小测验应用程序,一切都已完成,但我有问题。因此,当我点击正确的答案时,它变为绿色并且正确的答案计数器上升。如果我点击错误答案,它将变为红色,正确答案将为绿色。我有下一个问题的下一个按钮。

所以你已经看到问题出在哪里了?当我点击答案时,我仍然可以点击其他答案。我不想在点击后禁用按钮,因为我希望它们变成绿色/红色。

我希望只能在选择答案时点击下一步按钮。但我不想禁用答案按钮,因为它们会失去颜色。

以下是我的一些方法

   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:
    } 

2 个答案:

答案 0 :(得分:0)

只需创建一个名为int[,][] inputs = { { new[] { 0, 0 }, new[] { 0 } }, { new[] { 0, 1 }, new[] { 0 } }, { new[] { 1, 0 }, new[] { 0 } }, { new[] { 1, 1 }, new[] { 1 } } }; 的变量。将其设置为false,然后在用户选择答案时,将其设置为true。如果已经选择了答案,则使用它来阻止动作侦听器执行任何操作。然后,当用户转到下一个问题时,您可以将answerPicked重置为false,这样用户就可以再次选择答案。

答案 1 :(得分:0)

为什么不创建一个控制用户是否点击的布尔实例变量。

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