0

Currently, I'm programming a quiz game in Java using Swing. I also use the MVC Pattern and my ActionListener is placed in the control package.

I want a new question to show up after the current question is answered correctly. All answers are placed on buttons. I had the idea to compare the chosen answer with the question and if it is correct then the JLabel shall be filled with another question. I could put all questions into the label with a loop and interrupt the loop as soon as the chosen answer is wrong.

But I do not know how to implement it.

The comparison of the answer and the question happens in the ActionListener class and putting the question into the label is done in my panel class.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
random
  • 1
  • 2
  • *"I do not know how to implement it."* Then you need to consult your texts, notes and a tutor (at your school). This is not the place for such broad problems. – Andrew Thompson Mar 08 '22 at 19:26
  • Thanks for the advise but I do not go to school. I'm self taught. – random Mar 08 '22 at 20:07
  • 1
    [For example](https://stackoverflow.com/questions/31602113/listener-placement-adhering-to-the-traditional-non-mediator-mvc-pattern/31604919#31604919) – MadProgrammer Mar 08 '22 at 20:38
  • This is a complicated question. In a "traditional" MVC, the controller would be reacting to the events of the UI elements, but Swing is already a MVC, every control basically acts as it's own MVC, just wrapped up in a nice little package. I'd also argue that a controller and view should be decoupled, as the controller doesn't care how the view is implemented, they simply have an agreed contact via which they share information. This would "suggest" that the controller shouldn't be attaching an `ActionListener` to your controls. – MadProgrammer Mar 08 '22 at 20:43
  • Instead, the view would manage the events generated by the controls itself and, via the agreed contract, to the controller, ie "was the question answered correctly or not" – MadProgrammer Mar 08 '22 at 20:44

1 Answers1

-1

I'd prefer not to use loop. Try to work with Events. Register the event on the textfield (keyevent), and in the eventhandler you can compare the answers.

Uwe Allner
  • 3,399
  • 9
  • 35
  • 49