I'm trying to create a frame in Java using NetBeans. It will display some data and at the same time get data from the user in a while loop.
Can I pause the while
loop until a confirm button has been pressed? I found that someone may suggest sleep, but since I have to let the user enter the data, I can't predict how long they are going to take.
The event driven model of a GUI breaks sequential logic code.
To keep the sequential logic, one could maintain a state , for instance an enum:
enum State {
PROMPTING,
ASKING_TWO_FIELDS,
ASKING_THREE_FIELDS,
CALCULATING,
DONE
};
private State state = State.DONE;
(This explicit abstraction is often only needed for transition from sequential logic, or multiple states on the same forms.)
Then on button press and such change the state and disable/enable input components.
Events happen asynchrone, even possibly on different threads. One must write reactive code. And on preparing things wire together what should happen on a button press. This is very fragmentary.
One solution to this chaos is separating into Model-View-Control classes. Model classes contain the data, View are the GUI components, and one Controller, the application, is called from the views, sets the data, and controls the views.
Being called from the views is for swing done using listeners.
A recipe:
startTranslating
(text dropped on application), manualTranslationDone
(button "translated" pressed after manual editing).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.