Java - 当用户按下按钮时,暂停for循环并恢复迭代

时间:2012-02-03 00:27:57

标签: java button input for-loop

我正在使用Java中的SWT进行测验。我有一个包含问题和答案的课程。在主课程中,当用户通过Dialog输入他的姓名和问题数量并按OK时,测验开始。 我有一个for循环,应该遍历每个问题,例如,第一次迭代应该给用户一个问题,它应该等到用户通过文本框输入答案。当用户按下接下来的按钮时,如果答案正确,他将收到消息,然后转到第二次迭代,依此类推。 我的问题是,每次他开始测验时,for循环都没有等待用户输入和按钮预设动作,直接进入最后一个问题(for循环的最后一次迭代)。

有人可以给我一个例子,我该怎么做?

2 个答案:

答案 0 :(得分:1)

您需要更改问题编号以响应事件。以下是伪代码:

private int count = 0;

startMethod()
{
   askQuestion(0);
}

onTextInput()
{
    checkAnswer();
    count++;
    askQuestion(count);
}

答案 1 :(得分:1)

这是完整的代码。希望你会发现它很有用。

import org.eclipse.core.internal.databinding.Pair;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class Quiz {

    protected Shell shell;
    private Text text;
    private Pair[] questions ;
    private int number_of_question = 10;
    private int current_question = 0;
    public static void main(String[] args) {
        try {
            Quiz window = new Quiz();
            window.open();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while(!shell.isDisposed()) {
            if(!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    protected void createContents() {       
        createQuiz();   

        shell = new Shell();
        shell.setSize(450,300);
        shell.setText("SWT Application");

        final Label lblTheQuestion = new Label(shell, SWT.NONE);
        lblTheQuestion.setBounds(45, 38, 124, 15);
        lblTheQuestion.setText((String) questions[current_question].a);

        text = new Text(shell, SWT.BORDER);
        text.setBounds(45, 88, 76, 21);

        Button btnNext = new Button(shell, SWT.NONE);
        btnNext.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {

                if(text.getText().equals((String) questions[current_question].b)) {
                    new Thread(new Runnable() {
                        public void run() {                                 
                                shell.getDisplay().syncExec(new Runnable() {                                
                                    @Override
                                    public void run() {
                                        current_question++;
                                        lblTheQuestion.setText((String) questions[current_question].a);
                                        lblTheQuestion.redraw();    
                                    }
                                }); 
                        }

                    }).start();
                }       
            }
        });
        btnNext.setBounds(188, 55, 75, 25);
        btnNext.setText("Next");
    }
    private void createQuiz() { 
        questions = new Pair[number_of_question];
        for(int i = 0; i<number_of_question; i++) {
            questions[i] = new Pair("Question"+i,""+i);
        }

    }

}