Originally posted by Monk Fox:
ok, this program must be for school, cause its rediculous.
Monk, remember that the number one rule of JavaRanch is
Be Nice. This isn't a nice thing to say, nor is your response very helpful. You've had a lot to say without actually speaking to the question that was asked.
Now, Karl: perhaps without knowing it, you've wandered into Multithreaded Programming Territory. main() runs in one
thread, and GUI events are handled in another. You're wondering about how to communicate between them, and you've come up with a workable scheme (if not an optimal one.) Congratulations! Here lies power, but also responsibility. You need to make sure you understand what you're doing, but if you do, you can do neat stuff. First, I'm going to show you the "right" way to do the waiting that you're already doing; then I'm going to talk about other approaches.
First: instead of the "busy-wait" in main, you can use the wait() and notify() methods in Object.
Sun's threading tutorial will teach you all you need to know. Basically, main() would call wait(), where it would simply stop; and the event handler would call notify(), which would let main() proceed. None of the excess CPU time usage you're now seeing would happen.
Now: you've got code in main() running on one thread, and code in your GUI running on another, and so in your mind, threads and classes are intertwined a bit. Let me point out, though, that they're not, necessarily, and that the following plan lets you avoid the commnunication between threads, but still separate the question-management code into one class, and the GUI into another:
1) In your main class, you'd have methods to
-- Load questions: void loadQuestions()
-- choose a question: String getNextQuestion()
-- "grade" a question: boolean isRightAnswer(String question, String answer)
2) In your GUI class, you'd have all the setup code and event handlers.
3) main() would create a GUI instance and display it, perhaps with an initial question. Note that the GUI class can have a member variable that holds an instance of the main class, and the main class can pass an instance to the GUI class as a constructor argument. The GUI class can then use that member variable to call non-static methods on the main class.
4) (Here's the really new part) The event handler for the "accept my answer" button would call main.isRightAnswer(), and either get the next question by calling main.getNextQuestion() or not, depending on the return value.
Note that once the initial GUI is set up, the thread executing main() can terminate -- control is now handed over to the event-handling thread. But all the code that knows about the list of questions is in the main class, still, and the GUI can still use it.
Does this all make sense?