< Zurück | Inhalt | Weiter >

toolbar

A container for holding objects (usually buttons) which can attach to different sides of a window, or separate from a window and stand alone.


16.5 LAYOUT MANAGERS


Once you decided on all the graphical objects that you want to pack into your GUI, there is still one major hurdle—where to place all the objects. This can be one of the more challenging parts of GUI design because so many different screen sizes are available to the end user. How do you make your GUI look good to all (or most) of them?

Beginners often want to fix their components to absolute locations—for example, by sketching the GUI out on a piece of graph paper and then using those coordinates to fix the location. But with a windowing display, the user can grab a corner of the window and resize it. So, consider that piece of graph paper—what if it just got stretched to twice the size? Do you want all the con- trols to just get bigger? Buttons need not grow larger, but it would be nice to


have additional space contributed to text areas. Consider a word processor: When you resize its window you only want the text area to expand, not the menus and icons.

Layout managers are meant to address the issues of component placement and sizing. Some are very simple, others are much more complex. All have their advantages and disadvantages. We’ll use a few in our example to give you a taste of how they work.


16.6 BEYOND ARTHUR MURRAY: ACTIONS, LISTENERS, EVENTS


With any serious GUI comes a lot of advanced Java, especially constructs like anonymous inner classes. These are useful when we implement actions for our buttons and listeners for user selections.

Keep in mind that there are three different time frames that we’re consid- ering—compile time, construction time, and event time. Compile time is obvi- ous, and the compiler will complain if it cannot access what it needs due to scoping problems. Construction time is when the program constructs the GUI objects for display in a window using the new and add() calls as well as layout managers. Event time is when the user presses a button or edits text in a field, or when other external events change data.


16.7 GETTING DOWN TO CASES: DESIGNING A GUI FOR BUDGETPRO


Our goal is a GUI for the BudgetPro application. We’ve already built a com- mand-line version, so we want to have the same general functions but with the convenience and glamour of a GUI. The GUI will need to:

• Display the current account (name and dollars)

• Display the list of subaccounts that are part of this account

• Provide a way to create a new (sub)account

• Provide a way to move down into a subaccount

• Provide a way to move up to the parent account

From this brief list we devise a simple display with a heading that contains the current account and its dollar status, a table which lists the subaccounts,


and, at the bottom of the window, some buttons—one for creating new ac- counts and one for viewing subaccounts. Of course, we’ll also need a button to close or exit the application. In order to move back up from a subaccount, we’ll add a button up in the header, an up-arrow, which will take us back to the parent account.

Now we could show you a hand-drawn sketch of what this UI might look like, from our rough description, but we’ll cut to the chase and show you the finished product. This is the GUI for the BudgetPro application, in three stages. First comes the GUI when we first start up, with no subaccounts (Figure 16.2). When the New Subaccount button is pressed, a dialog will appear to collect the information need to create the subaccount, namely the new account’s

name and the dollar amount to allocate to that account (Figure 16.3).


image

Figure 16.2 BudgetPro GUI: top account window


image

Figure 16.3 BudgetPro GUI: creating a (sub)account



image

Figure 16.4 BudgetPro GUI: viewing subaccounts


Finally, Figure 16.4 is a look at the main GUI window for BudgetPro once several accounts have been constructed.

There is more to describe, but let’s walk through the code that builds this GUI and explain the parts as we go. The complete listing is available online at http://www.javalinuxbook.com. We’ll make reference to the line numbers of the listing in Appendix B, but sometimes we’ll show excerpts of the code as we go. You might find it handy to have a listing of the code (on paper or in your browser) as you read the next several sections.