Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

Summary 197

Row 2 of the interface is laid out into a grid that is two rows tall and seven
columns wide. The GridLayout()constructor also specifies that components
should be set apart from other components by 10 pixels in each direction.
Lines 54–55 set up this grid:


GridLayout layout2 = new GridLayout(2, 7, 10, 10);
row2.setLayout(layout2);


Row 4 uses GridLayoutto arrange components into a grid that is two rows
tall and three columns wide.


The LottoMadnessapplication uses several components described during this
hour. Lines 7–35 are used to set up objects for all the components that make up
the interface. The statements are organized by row. First, a JPanelobject for the
row is created, and then each component that goes on the row is set up. This
code creates all the components and containers, but they are not displayed
unless an add()method is used to add them to the application’s main frame.


In Lines 45–98, the components are added. Lines 45–52 are indicative of the
entireLottoMadness()constructor:


FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER,
10, 10);
option.add(quickpick);
option.add(personal);
row1.setLayout(layout1);
row1.add(quickpick);
row1.add(personal);
add(row1);


After a layout manager object is created, it is used with the setLayout()
method of the row’s JPanelobject—row1in this case. When the layout has
been specified, components are added to the JPanelby using its add()
method. After all the components have been placed, the entire row1object is
added to the frame by calling its own add()method.


Summary


When you design a Java program’s GUI for the first time, you might have
trouble believing that it’s an advantage for components to move around.
Layout managers provide a way to develop an attractive GUI that is flexible
enough to handle differences in presentation.


During the next hour, you learn more about the function of a GUI. You get a
chance to see the LottoMadnessinterface in use as it churns through lottery
drawings and tallies up winners.

Free download pdf