Core Swing components (III)

Source: Internet
Author: User

4.3 JLabel class

The first real Swing component that we need to look closely at is the simplest JLabel. JLabel is used as the replacement component of the AWT Label, but more things can be done. The AWT Label is only a single line of text. JLabel can be text, image, or both. The text can be a single line or HTML text. In addition, JLabel supports different pictures that are allowed and prohibited. Figure 4-6 shows some sample JLabel components.

4.3.1 create a JLabel

JLabel has six constructors:

public JLabel()JLabel label = new JLabel(); public JLabel(Icon image)Icon icon = new ImageIcon("dog.jpg");JLabel label = new JLabel(icon); public JLabel(Icon image, int horizontalAlignment)Icon icon = new ImageIcon("dog.jpg");JLabel label = new JLabel(icon, JLabel.RIGHT); public JLabel(String text)JLabel label = new JLabel("Dog"); public JLabel(String text, int horizontalAlignment)JLabel label = new JLabel("Dog", JLabel.RIGHT); public JLabel(String text, Icon icon, int horizontalAlignment)Icon icon = new ImageIcon("dog.jpg");JLabel label = new JLabel("Dog", icon, JLabel.RIGHT);

Through the JLabel constructor, We can customize three attributes of JLabel: text, icon or horizontalAlignment. By default, the text and icon attributes are empty, and the initial horizontalAlignment attribute settings depend on the parameters of the constructor. These settings can be JLabel. LEFT, JLabel. CENTER, or JLabel. RIGHT. In most cases, if horizontalAlignment is not specified, the left-aligned label is displayed. However, if only the initial icon is specified, the default alignment mode is center alignment.

4.3.2 JLabel attributes

Table 4-8 displays 14 JLabel attributes. These attributes allow us to customize the content, location, and behavior of JLabel.

JLabel attributes

Attribute name
Data Type

Accessibility

AccessibleContext
AccessibleContext

Read-Only

DisabledIcon
Icon

Read/write binding

DisplayedMnemonic
Char

Read/write binding

DisplaydMnemonicIndex
Int

Read/write binding

HorizontalAlignment
Int

Read/write binding

HorizontalTextPosition
Int

Read/write binding

Icon
Icon

Read/write binding

IconTextGap
Int

Read/write binding

LabelFor
Component

Read/write binding

Text
String

Read/write binding

UI
LabelUI

Read/write

UIClassID
String

Read-Only

VerticalAlignment
Int

Read/write binding

VerticalTextPosition
Int

Read/write binding

The content of JLabel is text and associated images. Displaying images in a JLabel will be discussed in the "interface Icon" section later in this chapter. However, we can display different icons based on whether JLabel is allowed or disabled. By default, if the allowed icons are from an Image object (ImageIcon, which will be discussed later in this chapter), the allowed icons are grayscale balanced. If the allowed icon does not come from an Image, there is no icon when the Jlabel is disabled, unless we manually specify it.

The location of the JLabel content is described by four different attributes: horizontalAlignment, horizontalTextPosition, verticalignment, and verticalTextPosition. The horizontalAlignment and verticalignment attributes describe the location of the JLabel content in its window.

The horizontal position can be the LEFT, RIGHT, or CENTER constant of the JLabel. Vertical positions can be in TOP, BOTTOM, or CENTER. Figure 4-7 shows alignment settings, and alignment is displayed through the icon.

When both text and icons are specified, the text position attribute reflects the text position relative to the icon. These attributes can be set to the same constant as the alignment attribute. Figure 4-8 shows various text attribute settings, which are reflected by the icon.

4.3.3 JLabel event handling

JLabel does not have specific event processing functions. In addition to the event processing functions inherited by JComponent, the combination of displaydMnemonic, displayedMnemonicIndex, and labelFor attributes is the most similar to event processing.

When the displayedMnemonic and labelFor attributes are set, the input focus is moved to the component associated with the labelFor attribute by pressing the specified key with the platform-related hotkey. When a component does not display its memory key settings in its own way, such as all input text components, this usage is very useful. The following is a demo. The running result is 4-9:

JLabel label = new JLabel("Username");JTextField textField = new JTextField();label.setDisplayedMnemonic(KeyEvent.VK_U);label.setLabelFor(textField);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.