Java Reference
In-Depth Information
Tip
This technique of developing small sections of code to show a concept is very useful in developing
and debugging code. It is often the case when attempting to incorporate some feature that does not appear
to work correctly, or trying to debug some existing code, that a large portion of the code is not relevant to
your problem. When you create a simple application just for testing the issue, you do not have irrelevant
code to distract you. It also means that you have a small bit of code with which to ask a friend or colleague
for help if necessary—asking a friend or colleague to help debug a thousand-line application is really push-
ing friendship.
■
The MainWindow
The
MainWindow
class extends
JFrame
and is the actual implementation of the Denny's DVDs
main window. The constructor of the
MainWindow
goes through the process of setting up the
application menu bar, the main data table, adding the
DVDScreen
(described after Listing 8-22),
and setting the main window in the center of the operating system screen.
First, the
DVDMainWindow
constructor creates an instance of its super class,
JFrame
, setting
the title of the application. Following this, a dialog box is created where the user can enter the
location of the database (described in the upcoming section, “Specifying the Database Loca-
tion”). The initialization procedure then continues to create the menu bar and all menu items.
As shown in Listing 8-21, there is one
JMenuBar
for the frame. The menu bar may have sev-
eral
JMenu
s attached (for example, one for the File menu, one for the Help menu, and so on).
Each menu may have several
JMenuItem
s attached—one for each action your user is likely to
perform via a menu.
Each menu and menu item may have an optional mnemonic key and an optional icon
attached. We have shown attaching the mnemonic key F to the File menu, and the mnemonic
key Q to the Quit menu item. If the user presses the Alt and F keys, the File menu will pop
down, and if they then press the Q key, the application will quit.
Normally each menu item has an
actionListener
attached to respond to events. We
have shown attaching an instance of the
QuitApplication
class to the
quitMenuItem
(the
QuitApplication
class will be shown in Listing 8-22).
After the menus have been configured, and the data loaded from the database, an
instance of the
DVDScreen
class is added to the
MainWindow
frame.
DVDScreen
is a
JPanel
that
contains the elements described in the “GUI Design and Layout” section earlier. It will be
shown in Listing 8-23.
Finally, an initial size for the application window is set, and the application window is
centered on the screen.
Listing 8-21.
The MainWindow Constructor: Setting Up the Menu
public MainWindow(String[] args) {
super("Denny's DVDs");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
ApplicationMode connectionType = (args.length == 0)
? ApplicationMode.NETWORK_CLIENT
: ApplicationMode.STANDALONE_CLIENT;