单击仅那些

Click just ones

我有一个JTextPane来显示调用Qus的问题,我有4个JLabel,A,B,C,D。在不同的页面上有五个问题。当您 select 正确答案 a、b、c 或 d 时,JLabel呼叫 Counter.

将增加 10 分

但我的问题是,当您单击 a、b、c、d 时,它会一次又一次地不断添加 10。我只希望它在每一页上添加 10 一次,如果下一页的答案正确,它应该在页面或答案上再添加 10 而不是 10 的倍数,那将是作弊。

这是代码

// To display the result
string preval = Counter.get text()+" ";
Counter.setText("0");

//Pls note Counter is to display result in the GUI
//Now the question method
Public void  init() {
    Call question==0;
}

//First Question
If(callquestion==1) {
    Qus.setText(" 1+1");
    A.setText("A) 2");
    B.setText("A) 8");
    C.setText("A) 9");
    D.setText("A) 10");
}

//Answer, let's assume the answer is A// note A, B, C, D are all JLables
Private void AmouseClicked(java.awt.event.MouseEvent evt) {
    If(callquestion==1 && D.isFocusable()) {
        Int d= Integer.parseInt(Counter.getText());
        Int e= 10;
        Int f=d+10;
        Counter.setText(f+" ");
    }
}

请注意,这只是 1 个问答。在我的项目中,我有 20 个这样的。我只希望每个按钮一次添加 10,并在多次点击时添加它 谢谢

我猜问题是什么:只要发生鼠标事件 (mouse_pressed、mouse_down、mouse_release),就会调用 AmouseClicked 方法。所以这应该可以解决问题。

 If(callquestion==1 && D.isFocusable())

需要

 If(callquestion==1 && D.isFocusable() && evt.getModifiersEx()==MouseEvent.MOUSE_PRESSED)

不是直接存储点,而是使用映射(问题,答案)来存储用户给出的问题和答案:

// somewhere in your main programme
List<Integer> userAnswers;
List<Integer> correctAnswers;

...

// at programme startup initialise to #answers
userAnswers = Arrays.toList(new Integer[20]); // create list full of null
correctAnswers = new ArrayList<Integer>();
for(int i = 0; i < 20; ++i)
    correctAnswers.add(0); // set all correct answers to #a (change this accordingly)

...

// when user answers question #q with answer #a
userAnswers.set(q, a); // set answer at #q to #a
Counter.setText(getScore() + " "); // update the counter every time the list changes!

...

// getScore counts up the awarded points for all questions
private int getScore() {
    int sum = 0;
    for(int q = 0; q < correctAnswers.size(); ++q) {
        int expected = correctAnswers.get(q); // assume initialised
        Integer actual = userAnswers.get(q); // may be null
        if(actual != null && actual == expected)
            sum += 10; // or have a third list indicating how many points each question awards
    }
}

这还有其他优势,比如您最终能够准确地告诉用户哪些答案他答对了哪些他答错了。

如果我理解正确,我相信这是解决您问题的更好方法:

您在设置中做了什么:

JLabel a = new JLabel()
a.addMouseListener(new MouseAdapter()  
{  
    public void mouseClicked(MouseEvent e)  
    {  
        aclicked();
    }  
});

JLabel b = new JLabel()
b.addMouseListener(new MouseAdapter()  
{  
    public void mouseClicked(MouseEvent e)  
    {  
        bclicked();
    }  
});
//do this for c and d as well

方法 aclicked 和 bclicked 是检查是否给出正确答案的地方,如果是,则奖励分数。

既然您一次要回答一个问题,为什么不重复使用该页面呢? :)

首先,我们创建一个问题class...

class Question{
    String question;
    String answer1, answer2, answer3, answer4;
    int userAnswer;
    int correctAnswer;
    boolean scoredPoints;
    Question(String q, int correct, String a1, String a2, String a3, String a4){
        this.question = q;
        this.correctAnswer = correct;
        this.answer1 = a1;
        this.answer2 = a2;
        this.answer3 = a3;
        this.answer4 = a4;

    }
}

然后我们可以创建问题...

Question q1 = new Question("1+1",1,"2","4","6","8");
//constructor is question, correct answer, ans 1, 2, 3, 4.

然后创建一个问题列表...

ArrayList<Question> questions = new ArrayList<>();

并将问题添加到列表中...

questions.add(q1);

然后你做同样的事情来提出问题,但是,你会引用 questions.get(x) 其中 x 是问题编号(减一,因为数组索引 0 等于针对对象 1)。

当用户按下按钮(例如 A)时,您调用 actionlistener 并调用:

questions.get(x).scoredPoints = true; //prevents cheating
questions.get(x).userAnswer = buttonNumber; //sets user question answer

x 在这种情况下是当前问题编号。

这样你就可以源源不断地提问,他们的回答会被记录和检查,他们不能"cheat"。