Java Reference
In-Depth Information
f.add(createGUI());
f.pack();
f.setResizable(false);
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}
Listing 7-2
presents an alternative architecture for creating a GUI. Instead of sub-
classing
Frame
,thisclassisinstantiateddirectlyandvariousmethodsarecalledtocon-
figure and display the frame window.(It's convenient to create a class method such as
createGUI()
that returns a
Panel
object containing the entire GUI. The returned
Panel
instance is passed to
Frame
's
add()
method to install the GUI.)
Figure 7-5
reveals the improved GUI.
Figure 7-5.
A nicer looking GUI is achieved by wrapping components in nested panels.
Notice that the components are displayed at their preferred sizes. This is caused by
adding a label and a textfield, or by adding the two buttons to a nested panel (whose
layout manager is flow), and then adding this panel to the main layout panel. (A flow
layout lets each component assume its natural [preferred] size.)
Although
Figure 7-5
'
s GUI looks nicer than the GUI shown in
Figure 7-4
, there's
roomforimprovement.Forexample,wecouldleft-aligntheDegreesandResultlabels
andthetextfields.Wecouldalsoensurethateachbuttonhasthesamesize.
Figure7-6
shows you what the resulting GUI would look like.