26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

904 Files and Streams Chapter 16<br />

defined at lines 34–75. Methods getFieldValues (lines 116–124), setField-<br />

Values (lines 104–113) and clearFields (lines 96–100) manipulate the text of the<br />

JTextFields. Methods getFields (lines 90–93), getDoTask1But<strong>to</strong>n (lines 78–<br />

81) and getDoTask2But<strong>to</strong>n (lines 84–87) return individual GUI components, so that a<br />

client program can add ActionListeners (for example).<br />

1 // Fig. 16.4: BankUI.java<br />

2 // A reusable GUI for the examples in this chapter.<br />

3 package com.deitel.jhtp4.ch16;<br />

4<br />

5 // <strong>Java</strong> core packages<br />

6 import java.awt.*;<br />

7<br />

8 // <strong>Java</strong> extension packages<br />

9 import javax.swing.*;<br />

10<br />

11 public class BankUI extends JPanel {<br />

12<br />

13 // label text for GUI<br />

14 protected final static String names[] = { "Account number",<br />

15 "First name", "Last name", "Balance",<br />

16 "Transaction Amount" };<br />

17<br />

18 // GUI components; protected for future subclass access<br />

19 protected JLabel labels[];<br />

20 protected JTextField fields[];<br />

21 protected JBut<strong>to</strong>n doTask1, doTask2;<br />

22 protected JPanel innerPanelCenter, innerPanelSouth;<br />

23<br />

24 // number of text fields in GUI<br />

25 protected int size;<br />

26<br />

27 // constants representing text fields in GUI<br />

28 public static final int ACCOUNT = 0, FIRSTNAME = 1,<br />

29 LASTNAME = 2, BALANCE = 3, TRANSACTION = 4;<br />

30<br />

31 // Set up GUI. Construc<strong>to</strong>r argument of 4 creates four rows<br />

32 // of GUI components. Construc<strong>to</strong>r argument of 5 (used in a<br />

33 // later program) creates five rows of GUI components.<br />

34 public BankUI( int mySize )<br />

35 {<br />

36 size = mySize;<br />

37 labels = new JLabel[ size ];<br />

38 fields = new JTextField[ size ];<br />

39<br />

40 // create labels<br />

41 for ( int count = 0; count < labels.length; count++ )<br />

42 labels[ count ] = new JLabel( names[ count ] );<br />

43<br />

44 // create text fields<br />

45 for ( int count = 0; count < fields.length; count++ )<br />

46 fields[ count ] = new JTextField();<br />

Fig. Fig. 16.4 16.4 BankUI contains a reusable GUI for several programs (part 1 of 3).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!