kaydrae kaydrae - 6 months ago 12
Java Question

Is there a way I can stop the program and wait for the user to click a button

The problem is that when the button is clicked and enters the start game method, the program does not wait for the action listener that is included in the question methods. Instead it skips to the last method. Thanks.

//Setting Main Layout
Game.setLayout(new BorderLayout());
Game.add(NorthName, NORTH);
Game.add(SouthScore, SOUTH);
Game.add(Center, CENTER);

//Setting NorthName Layout
NorthName.setLayout(new GridLayout(0,2,2,0));
NorthName.add(name);
NorthName.add(getName);

//Setting SouthScore Layout
SouthScore.setLayout(new FlowLayout());
SouthScore.add(scoreL);

scoreL.setText("Score: " + String.valueOf(scoreN) + "/10");

//Setting Center
Center.setLayout(new GridLayout(4,3,0,0));
Center.add(intro);
Center.add(question);
Center.add(True);
Center.add(False);
Center.add(status);
Center.add(answer);


True.setText("Start");
False.setText("Exit");
intro.setText("");
question.setText("");
status.setText("");
answer.setText("");


True.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

intro.setText("Hello " + getName.getText());
True.setText("True");
False.setText("False");

status.setText("");
answer.setText("");

startGame();

}
});

False.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

intro.setText("Hello guest");
True.setText("True");
False.setText("False");

status.setText("Well... ");
answer.setText("That's too bad. ;)");

startGame();

}
});



}


start game methods

void startGame(){

scoreN = 0;

setQuestionOne();
setQuestionTwo();
setQuestionThree();
setQuestionFour();
setQuestionFive();
setFinalScore();

}


This is what is in the set question methods is the same through out each one.

void setQuestionOne(){

question.setText("i'Robot is about Robots.");

True.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

status.setText("Correct! +3");
answer.setText("This Movie is about robots!");
scoreN = scoreN + 3;
scoreL.setText("Score: " + String.valueOf(scoreN) + "/10");

}
});

False.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

status.setText("Incorrect! +0");
answer.setText("This Movie is about robots!");
scoreN = scoreN + 0;

}
});


}

Answer

Use a JOptionPane. You can display each question separately in a JOptionPane. The dialog will only close when the user responds to the dialog.

Read the section from the Swing tutorial on How to Make Dialogs for working examples.

Otherwise you need to redesign your form. That is you would display a single question. Then in the ActionListener for the first question, you would display the second question and so on. In the ActionListener for the second question you display the third question.

Or in your ActionListener you would need to invoke a "Next Question" method. So in this case you would use an ArrayList to hold each question. Then every time a question is answered, you move to the next question in the ArrayList. This is the better approach because you can easily change the number of questions you want to ask. You should never really hard code methods to ask a different question.

Also, variable names should NOT start with an upper case character. Some of you variable names are correct, others are not. Be consistent!

Comments