Java Reference
In-Depth Information
LISTING 14.3
Continued
10: JTextField[] total = new JTextField[16];
11: // the “Roll” button
12: JButton roll;
13: // the number of times to roll
14: JTextField quantity;
15: // the Swing worker
16: DiceWorker worker;
17:
18: public DiceRoller() {
19: super(“Dice Roller”);
20: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21: setSize(800, 125);
22:
23: // set up top row
24: JPanel topPane = new JPanel();
25: GridLayout paneGrid = new GridLayout(1, 16);
26: topPane.setLayout(paneGrid);
27: for (int i = 0; i < 16; i++) {
28: // create a textfield and label
29: total[i] = new JTextField(“0”, 4);
30: JLabel label = new JLabel((i + 3) + “: “);
31: // create this cell in the grid
32: JPanel cell = new JPanel();
33: cell.add(label);
34: cell.add(total[i]);
35: // add the cell to the top row
36: topPane.add(cell);
37: }
38:
39: // set up bottom row
40: JPanel bottomPane = new JPanel();
41: JLabel quantityLabel = new JLabel(“Times to Roll: “);
42: quantity = new JTextField(“0”, 5);
43: roll = new JButton(“Roll”);
44: roll.addActionListener(this);
45: bottomPane.add(quantityLabel);
46: bottomPane.add(quantity);
47: bottomPane.add(roll);
48:
49: // set up frame
50: GridLayout frameGrid = new GridLayout(2, 1);
51: setLayout(frameGrid);
52: add(topPane);
53: add(bottomPane);
54:
55: setVisible(true);
56: }
57:
58: // respond when the “Roll” button is clicked
14
Search WWH ::




Custom Search