2.3 The Label class

An instance of the Java Label class was used in the previous chapter to provide the area upon which the value of a ClickCounter was displayed. This is a relatively simple class and provides a convenient point to start the tour. Table 2.1 lists the major methods and other resources of the class.

Table 2.1 Major resources of the Label class.

The data attributes of a Label instance are the text which it will display and the alignment of the text. The possible alignments for the text are enumerated by the three Label class wide constants CENTER, LEFT and RIGHT, with a default of LEFT when it is not explicitly specified otherwise. The four public methods provide inquiry and modification methods for these two data attributes, allowing the value of each of the attributes to be retrieved from the object or a new value to be specified. The setText() method was used in the previous chapter whenever the value of the ClickCounter changed, in order to display the new value. This class does not have to explicitly provide attributes for resources such as the foreground and background colors or the font to be used as these are inherited from the Component class, as will be explained below.

The following code fragment, showing the init() method of a class called LabelExample which extends the Applet class, creates three Label instances illustrating each of the possible alignments.

0020      public void init() { 0021       0022      Label  leftLabel    = new Label( "Alignment left",   Label.LEFT); 0023      Label  centerLabel  = new Label( "Alignment center", Label.CENTER); 0024      Label  rightLabel   = new Label( "Alignment right",  Label.RIGHT); 0025       0026         this.setLayout( new GridLayout( 3, 1, 0, 10)); 0027          0028         this.add( leftLabel); 0029         this.add( centerLabel); 0030         this.add( rightLabel); 0031      } // End init.

The layout policy of the LabelExample Panel is specified as a 3 row by 1 column GridLayout and so the three Labels are maintained in single column, from top to bottom in the sequence in which they were added, producing the appearance shown in Figure 2.3.

Figure 2.3 Three Label instances illustrating different alignments.



2.4 The Component class

2.2 The AWT class hierarchy

A Java GUI programmer's primer
Java GUI Programmers Primer, A
ISBN: 0139088490
EAN: 2147483647
Year: 1998
Pages: 85
Authors: Fintan Culwin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net