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

(singke) #1
ptg7068951

Laying Out an Application 193

The next project develops a full interface for the program you write during
the next hour. The program is a Lotto number cruncher that assesses a
user’s chance of winning one of the multimillion dollar Lotto contests in
the span of a lifetime. This chance is determined by running random six-
number Lotto drawings again and again until the user’s numbers turn up
as the big winner. Figure 14.6 shows the GUI you are developing for the
application.


FIGURE 14.6
Displaying the GUI of the
LottoMadnessapplication.

Create a new empty Java file called LottoMadness, enter text from Listing
14.2 into the source editor, and save the file.


LISTING 14.2 The Full Text of LottoMadness.java
1: importjava.awt.;
2: importjavax.swing.
;
3:
4: public classLottoMadness extendsJFrame {
5:
6: // set up row 1
7: JPanel row1= new JPanel();
8: ButtonGroup option= new ButtonGroup();
9: JCheckBox quickpick= newJCheckBox(“Quick Pick”, false);
10: JCheckBox personal= new JCheckBox(“Personal”, true);
11: // set up row 2
12: JPanel row2= new JPanel();
13: JLabel numbersLabel= newJLabel(“Your picks: “, JLabel.RIGHT);
14: JTextField[] numbers= new JTextField[6];
15: JLabel winnersLabel= newJLabel(“Winners:“, JLabel.RIGHT);
16: JTextField[] winners= new JTextField[6];

Free download pdf