|
|
|
|
|
|
RegistrationForm HelpHelp is available for most tasks, or you can go straight to the RegistrationForm source code to see a solution.
These are all derived from FormElement. You will want to set the layout manager for mainPanel so that it lays out the compound components per the expected behavior. Assume that the required classes exist and then define references to them in RegistrationForm.
In the init method, you need to create a Panel (mainPanel variable) that contains all of these objects. What should the layout be for the new Panel? Unfortunately, the commonly used AWT layout managers do not isolate the specification of size and position. For example, centering a Component over another Component is straightforward using BorderLayout, but the size of the components would be stretched to the extent of the Container! You can often get around this by using FlowLayout and making sure that the Container has the right size so that the Components wrap correctly, thus, getting the layout you want. This is the approach you can take for this main panel.
Creating a title and Submit Button for the applet is easy where the setFont method is used to change the Font type, weight, and size:
The Submit Button needs to be defined as an instance variable so that the action method can refer to it also:
The overall applet layout has the title at the top, the mainPanel in the center, and the Submit Button at the bottom. As mentioned earlier, centering objects is easy with BorderLayout or GridLayout, but this stretches the components; for example, the Button would end up being as wide as the applet--an unattractive option. There is little choice in this situation, but to use a layout manager other than those provided by the AWT (note at this point that AWT's GridBagLayout could handle this situation, but is extremely complicated and beyond the scope of this short course). You will use a simple layout manager called RatioLayout that specifies the position and size of components as ratios of the Container size; a ratio of "c" indicates that you would like to have the Component centered in that dimension.
First you must check to see that the Submit Button was pushed, which is easily done by comparing the target of the Event with the Button you created. Next, you must ask course, contact, and job if they are empty. If any are empty, report an error to the System.err stream. Otherwise, ask each object for its contents and print them to System.out. Both isEmpty and getContents are methods defined by FormElement. If the Submit Button is pushed, return true indicating that you handled the event, otherwise return false. See Events for more information on events and event-chain flow. Here is the complete action method:
|