![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
GrayFilter ClassThe GrayFilter class is a utility class used to create grayed out versions of images. This class is an extension of the AWT class RGBImageFilter. In the JFC, the GrayFilter class is used in components containing icons. If a disabled image is not specified, the component creates an image from the normal image to use when the component is disabled. Calling the static createDisabledImage method contained in this class performs this function. The following code fragment creates a disabled icon from an image. Notice that the createDisabledImage takes an image and returns an image. As in the previous section, an ImageIcon is created from the image and used wherever an icon is required. The createDisabledImage method is used internally by Swing components, but can also be used by any class to create grayed out images. Icon disabledIcon = new ImageIcon( GrayFilter.createDisabledImage( myImage ) ); Label ComponentsThe JFC contains many components that contain a label. This section presents the simple labeled components. You will learn how to do the following:
The JLabel ClassThe JLabel class extends JComponent. It provides a display area for text, an icon, or both. The JLabel class does not respond to input events and cannot obtain the input focus. The most common use for the JLabel class is to display a single line of text. A JLabel can be created with a String parameter for this purpose as shown in the following: JLabel label = new JLabel( Text on label ); This label will be left aligned and horizontally centered in its display area. The horizontal alignment can be specified at construction time as shown in the following line of code: JLabel label = new JLabel( Text on label, SwingConstants.CENTER ); This will create a horizontally centered label. Other possible horizontal alignments are SwingConstants.LEFT, SwingConstants.LEADING, SwingConstants.RIGHT, and SwingConstants.TRAILING. The horizontal alignment can be set after creation with the setHorizontalAlignment method and passing it the desired alignment. The horizontal alignment is a bound property named horizontalAlignment. Notice how the JLabel class takes advantages of the constants in the SwingConstants interface rather than defining the constants in the JLabel class itself. This is inconsistent with what you saw in the TitledBorder class presented in the previous section. The horizontal alignment can be queried with the getHorizontalAlignment method. It is also unfortunate that the property names in JLabel are not class constants. Instead, they are hard coded into the source code. The vertical alignment can be set with setVerticalAlignment. Possible alignments are SwingConstants.TOP, SwingConstants.BOTTOM, and the default, SwingConstants.CENTER. The vertical alignment can only be set after the label is created. There is not a version of the constructor with a vertical alignment parameter. The vertical alignment is a bound property named verticalAlignment. The vertical alignment can be queried using the getVerticalAlignment method. The application presented in Listing 5.6 creates a panel containing JLabel instances with the possible combinations for vertical and horizontal text positions. The application simply creates the JLabel instances and sets the appropriate properties. The labels are arranged in a 4×4 grid where the first column and top rows are labels for the position of the text. An AWT GridLayout layout manager is used for creating the grid. This ensures that each cell is the same size. A strut is used for the 0,0 position in the grid that does not contain a label. The resulting frame is presented in Figure 5.12. As can be seen from the figure, the JLabel is very flexible with the text alignment. Listing 5.6 LabelTest Application package com.foley.test; import java.awt.*; import javax.swing.*; import javax.swing.border.*; import com.foley.utility.ApplicationFrame; /** * An application that displays possible label placement * in a frame. * * @author Mike Foley **/ public class LabelTest extends Object { /** * Create a panel containing possible label positions. * * @return A panel containing many titled borders. **/ protected static JPanel createLabelPanel() { // // Labels // int strutHeight = 10; JPanel labelPanel = new JPanel(); labelPanel.setLayout( new GridLayout( 4, 4 ) ); labelPanel.add( Box.createVerticalStrut( strutHeight ) ); JLabel title = new JLabel( TOP, SwingConstants.CENTER ); title.setVerticalAlignment( SwingConstants.BOTTOM ); labelPanel.add( title ); title = new JLabel( CENTER, SwingConstants.CENTER ); title.setVerticalAlignment( SwingConstants.BOTTOM ); labelPanel.add( title ); title = new JLabel( BOTTOM, SwingConstants.CENTER ); title.setVerticalAlignment( SwingConstants.BOTTOM ); labelPanel.add( title ); title = new JLabel( RIGHT, SwingConstants.RIGHT ); labelPanel.add( title ); title = new JLabel( Top Right, SwingConstants.RIGHT ); title.setVerticalAlignment( SwingConstants.TOP ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); labelPanel.add( title ); title = new JLabel( Center Right, SwingConstants.RIGHT ); title.setVerticalAlignment( SwingConstants.CENTER ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); labelPanel.add( title ); title = new JLabel( Bottom Right, SwingConstants.RIGHT ); title.setVerticalAlignment( SwingConstants.BOTTOM ); labelPanel.add( title ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); title = new JLabel( CENTER, SwingConstants.RIGHT ); labelPanel.add( title ); title = new JLabel( Top Center, SwingConstants.CENTER ); title.setVerticalAlignment( SwingConstants.TOP ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); labelPanel.add( title ); title = new JLabel( Center Center, SwingConstants.CENTER ); title.setVerticalAlignment( SwingConstants.CENTER ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); labelPanel.add( title ); title = new JLabel( Bottom Center, SwingConstants.CENTER ); title.setVerticalAlignment( SwingConstants.BOTTOM ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); labelPanel.add( title ); title = new JLabel( LEFT, SwingConstants.RIGHT ); labelPanel.add( title ); title = new JLabel( Top Left, SwingConstants.LEFT ); title.setVerticalAlignment( SwingConstants.TOP ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); labelPanel.add( title ); title = new JLabel( Center Left, SwingConstants.LEFT ); title.setVerticalAlignment( SwingConstants.CENTER ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); labelPanel.add( title ); title = new JLabel( Bottom Left, SwingConstants.LEFT ); title.setVerticalAlignment( SwingConstants.BOTTOM ); title.setBorder( BorderFactory.createLineBorder( Color.black ) ); labelPanel.add( title ); return( labelPanel ); } /** * Application entry point. * Create the frame, and display it. * * @param args Command line parameter. Not used. **/ public static void main( String args[] ) { JFrame frame = new ApplicationFrame( Label Position Test ); JPanel labels = createLabelPanel(); // // Add the border panels to the content. // labels.setBorder( BorderFactory.createTitledBorder( BorderFactory.createBevelBorder(BevelBorder.LOWERED), Label Positions ) ); frame.getContentPane().add( labels, BorderLayout.CENTER ); frame.pack(); frame.setVisible( true ); } // main } // LabelTest
|
![]() |
|