![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
The text displayed in the JLabel can be altered after construction using the setText method. The JLabel text is the bound property named text. The text can be queried with the getText method. The JLabel class is a descendant of the AWT Component class. JLabel inherits methods for setting the foreground and background colors and the font with which to display the label. However, JLabel, as with all the JFC components, inherits from JComponent. As described in the JComponent chapter, these are lightweight components. Thus, the default behavior of JLabel is to not draw its background. The background of its container is used instead. The side effect of this is that setting the background color of a label has no effect. To force the JLabel class to paint with the background color specified, the label must be made opaque. Calling the setOpaque method with a parameter of true does this. The current opaque setting can be queried with the isOpaque method. The opaque setting for the JLabel is a bound property named opaque. The setFont method is overridden to force the label to be repainted after the font changes. This allows setting the font to behave consistently with setting other label properties that repaint the label after their value changes. Adding an Image As was mentioned in the introduction to this section, a JLabel can display an icon as well as a string. The icon can be specified in the JLabel constructor or set later using the setIcon method. The icon is the bound property named icon. The following code demonstrates creating a JLabel with an icon. JLabel iconLabel = new JLabel( myIcon ); As with a JLabel containing a string, the icons alignment can be specified. The default alignment for the icon-only JLabel is to center the icon both horizontally and vertically in its display space. The same alignment methods presented in the previous section for string-only labels apply to icon labels. For example, the following code fragment will create a JLabel containing an icon in the bottom-right portion of the JLabels display area. JLabel iconLabel = new JLabel( myIcon, SwingConstants.RIGHT ); iconLabel.setVerticalAlignment( SwingConstants.BOTTOM ); A single JLabel can contain both an icon and a string. Both the icon and string can be specified at construction, as shown in the following code fragment. Either the string or icon can be specified after creation with the setIcon or setText method, respectively. JLabel label = new JLabel( Test, myIcon, SwingConstants.RIGHT ); This label will use the default string-to-icon placement, which is to place the string to the right of the icon. A default gap of four pixels is placed between the text and the image. However, the positioning property can be altered with the setHorizontalTextPosition method. The possible valuesRIGHT, CENTER, LEFT, LEADING, and TRAILINGare defined in the SwingConstants interface. This is a bound property namehorizontalTextPosition. The current value of this property can be queried with the getHorizontalTextPosition method. Similarly, the vertical position of the text relative to the icon can be specified with the setVerticalTextPosition method. Possible values for this propertyTOP, CENTER, and BOTTOMare defined in the SwingConstants interface. The vertical text position is a bound property named verticalTextPosition. The current value of this property can be queried with the getVerticalTextPosition method. The gap between the icon and string can be specified with the setIconTextGap method. This method will set the distance between the icon and the string to the given number of pixels. The distance between the icon and the string is a bound property named iconTextGap. As previously mentioned, the default value for this property is four pixels. The alignment properties, previously described for a label containing text, move the text and icon as a unit. Setting the horizontalAlignment and verticalAlignment properties allows the text and icon to be positioned in the label. The application presented in Listing 5.7 creates a panel containing JLabel instances with the various text-to-icon placements specified. This application follows the same general structure as the application in the previous section. The difference is that icons are added to each label, and the text-to-icon placement is specified. The resulting display is shown in Figure 5.13. In this example, the horizontal alignment for each label is CENTER. Listing 5.7 LabelAndIconTest Application package com.foley.test; import java.awt.*; import javax.swing.*; import javax.swing.border.*; import com.foley.utility.ApplicationFrame; import com.foley.utility.ImageLoader; /** * An application that displays possible label placement * in its frame. * * @author Mike Foley **/ public class LabelAndIconTest extends Object { private static Icon stop; /** * Create a panel containing possible label positions. * * @return A panel containing many titled borders. **/ private static JPanel createLabelAndIconPanel() { // // Labels and icons. // int strutHeight = 3; strutHeight = 10; JPanel labelAndIcon = new JPanel(); labelAndIcon.setLayout( new GridLayout( 4, 4 ) ); labelAndIcon.add( Box.createVerticalStrut( strutHeight ) ); JLabel title = new JLabel( TOP, SwingConstants.CENTER ); title.setVerticalAlignment( SwingConstants.BOTTOM ); labelAndIcon.add( title ); title = new JLabel( CENTER, SwingConstants.CENTER ); title.setVerticalAlignment( SwingConstants.BOTTOM ); labelAndIcon.add( title ); title = new JLabel( BOTTOM, SwingConstants.CENTER ); title.setVerticalAlignment( SwingConstants.BOTTOM ); labelAndIcon.add( title ); title = new JLabel( RIGHT, SwingConstants.RIGHT ); labelAndIcon.add( title ); title = new JLabel( Top Right, stop, SwingConstants.CENTER ); title.setHorizontalTextPosition( SwingConstants.RIGHT ); title.setVerticalTextPosition( SwingConstants.TOP ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); labelAndIcon.add( title ); title = new JLabel( Center Right, stop, SwingConstants.CENTER ); title.setHorizontalTextPosition( SwingConstants.RIGHT ); title.setVerticalTextPosition( SwingConstants.CENTER ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); labelAndIcon.add( title ); title = new JLabel( Bottom Right, stop, SwingConstants.CENTER ); title.setHorizontalTextPosition( SwingConstants.RIGHT ); title.setVerticalTextPosition( SwingConstants.BOTTOM ); labelAndIcon.add( title ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); title = new JLabel( CENTER, SwingConstants.RIGHT ); labelAndIcon.add( title ); title = new JLabel( Top Center, stop, SwingConstants.CENTER ); title.setHorizontalTextPosition( SwingConstants.CENTER ); title.setVerticalTextPosition( SwingConstants.TOP ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); labelAndIcon.add( title ); title = new JLabel( Center Center, stop, SwingConstants.CENTER ); title.setHorizontalTextPosition( SwingConstants.CENTER ); title.setVerticalTextPosition( SwingConstants.CENTER ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); labelAndIcon.add( title ); title = new JLabel( Bottom Center, stop, SwingConstants.CENTER ); title.setHorizontalTextPosition( SwingConstants.CENTER ); title.setVerticalTextPosition( SwingConstants.BOTTOM ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); labelAndIcon.add( title ); title = new JLabel( LEFT, SwingConstants.RIGHT ); labelAndIcon.add( title ); title = new JLabel( Top Left, stop, SwingConstants.LEFT ); title.setHorizontalTextPosition( SwingConstants.LEFT ); title.setVerticalTextPosition( SwingConstants.TOP ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); labelAndIcon.add( title ); title = new JLabel( Center Left, stop, SwingConstants.LEFT ); title.setHorizontalTextPosition( SwingConstants.LEFT ); title.setVerticalTextPosition( SwingConstants.CENTER ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); labelAndIcon.add( title ); title = new JLabel( Bottom Left, stop, SwingConstants.LEFT ); title.setHorizontalTextPosition( SwingConstants.LEFT ); title.setVerticalTextPosition( SwingConstants.BOTTOM ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); labelAndIcon.add( title ); return( labelAndIcon ); } /** * Application entry point. * Create the frame, label panel, * and display them. * * @param args Command line parameter. Not used. **/ public static void main( String args[] ) { try { // // Load the images used. // stop = ImageLoader.loadIcon( stop.gif ); } catch( InterruptedException ie ) { System.err.println( Error loading images ); System.exit( -1 ); } JFrame frame = new ApplicationFrame( Label to Icon Position Test ); JPanel labels = createLabelAndIconPanel(); // // Add the border panels to the content. // labels.setBorder( BorderFactory.createTitledBorder( BorderFactory.createBevelBorder( BevelBorder.LOWERED ), Label to Icon Positions ) ); frame.getContentPane().add( labels, BorderLayout.CENTER ); frame.pack(); frame.setVisible( true ); } // main } // LabelAndIconTest
|
![]() |
|