Java swing label controls and Icon controls

Source: Internet
Author: User
Tags file url

Labels are used to display text or prompt information in Swing. They support text strings and icons. In the application's user interface, a short text label can let users know the purpose of these controls, so labels are commonly used in Swing.

1. Use of tags

The label is defined by the JLabel class, and the parent class is the JComponent class.

A label can display a row of read-only text, an image, or text with an image. It does not generate any type of events, but simply displays text and images at a low level, however, you can use the Tag feature to specify the text alignment mode on the tag.

The JLabel class provides multiple constructor methods to create multiple labels, such as displaying only text labels, only icon labels, or labels containing text and icons, several common JLabel constructor methods are shown in the table.

JLabek ()

Create a JLabel object without icons and text

JLabel (Icon)

Create a JLabel object with icons

JLabel (Icon, int aligment)

Create a JLabel object with icons and set horizontal alignment

JLabel (String text, int aligment)

Create a JLabel object with text and set horizontal alignment

JLabel (String text, Icon icon, int aligment)

Create a JLabel object with text and icons and set the horizontal alignment of the label content

2. Use of icons

Icons in Swing can be placed on buttons and label controls to describe the purpose of controls. Icons can be created by the image file types supported by Java, or java. awt. graphics class provides Function Methods to draw

In addition to drawing icons in Swing, you can also create icons using a specific image, using javax. swing. the ImageIcon class allows you to create icons based on existing images. The ImageIcon class implements the Icon interface, and java supports multiple image formats.

Method Name


ImageIcon ()

Create a common ImageIcon object. When you need to set an Image, call the setImage (image Image) method to specify the image.

ImageIcon (Image image)

Create an icon directly from the image source

ImageIcon (Image image, Strign description)

In addition to creating an icon from an image source, you can also add a brief description for the icon, but the description is not displayed on the icon. You can use the getDecription () method to obtain the description.

ImageIcon (URL)

Create an icon using the Image File URL on the computer network.

/***** @ Author gao */package com. gao; import java. awt. borderLayout; import javax. swing. imageIcon; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. JPanel; import javax. swing. border. emptyBorder; public class JLabelDemo extends JFrame {private JPanel contentPane; private JLabel label; public JLabelDemo () {this. setTitle ("tag containing icons"); this. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); this. setBounds (100,100,100 0, 600); contentPane = new JPanel (); contentPane. setBorder (new EmptyBorder (5, 5, 5); contentPane. setLayout (new BorderLayout (0, 0); this. setContentPane (contentPane); label = new JLabel ("Today is July 22, May 1, 2014, Labor Day"); label. setIcon (new ImageIcon ("1.jpg"); contentPane. add (label, BorderLayout. CENTER); this. setVisible (true);} public static void main (String [] args) {JLabelDemo example = new JLabelDemo ();}}
Running result:

Related Article

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: 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.