Java Programming swing component JLabel details and use examples

  • 2020-12-16 05:56:09
  • OfStack

The JLabel object can display text, images, or both. You can specify where the label content is aligned in the label display area by setting vertical and horizontal alignment. By default, labels are vertically centered in their display area. By default, labels that display only text are aligned to the start edge; Labels that show only images are horizontally centered. You can also specify the position of the text relative to the image. By default, the text is at the end edge of the image, and the text and image are aligned vertically.

Introduction to construction method:

JLabel() creates JLabel with no image and an empty string title.

JLabel(Icon image) creates an instance of JLabel with the specified image.

JLabel(Icon image, int horizontalAlignment) creates an instance of JLabel with the specified image and horizontal alignment.

JLabel(String text) creates an instance of JLabel with the specified text.

JLabel(String text, Icon icon, int horizontalAlignment) creates an instance of JLabel with the specified text, image, and horizontal alignment.

JLabel(String text, int horizontalAlignment) creates an instance of JLabel with the specified text and horizontal alignment.

Common methods:

getHorizontalAlignment() returns the alignment of the tag contents along the X axis.

getHorizontalTextPosition() returns the horizontal position of the text of the tag relative to its image.

getIcon() returns the graphic image (glyph, icon) displayed by the label. getText() returns the text string displayed by the tag.

setHorizontalAlignment(int alignment) sets the alignment of the label content along the X axis.

setHorizontalTextPosition(int textPosition) sets the horizontal position of the text of the label relative to its image.

setIcon(Icon icon) defines the icon that this component will display.

setText(String text) defines the single line of text that this component will display. setUI(LabelUI ui) sets L to render this component & F object.

setVerticalAlignment(int alignment) sets the alignment of the label content along the Y axis.

setVerticalTextPosition(int textPosition) sets the vertical position of the text of the label relative to its image.

Add images and text to JLabel


import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class MixingIconLabel {
	public static void main(String[] args) {
		JFrame.setDefaultLookAndFeelDecorated(true);
		JFrame frame = new JFrame();
		frame.setTitle("JLabel Test");
		frame.setLayout(new FlowLayout());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ImageIcon imageIcon = new ImageIcon("yourFile.gif");
		JLabel label = new JLabel("Mixed", imageIcon, SwingConstants.RIGHT);
		frame.add(label);
		frame.pack();
		frame.setVisible(true);
	}
}

HTML text added to JLabel


import javax.swing.JFrame;
import javax.swing.JLabel;
public class HTMLLabel {
	public static void main(String[] a) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JLabel label = new JLabel("<html>bold <br> plain</html>");
		frame.add(label);
		frame.setSize(300, 200);
		frame.setVisible(true);
	}
}

Rewrite JLabel


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.Serializable;
import javax.swing.JLabel;
public class Colors extends JLabel implements Serializable {
	transient private Color color;
	// not persistent 
	private Boolean rectangular;
	// is persistent 
	public Colors() {
		addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent me) {
				change();
			}
		}
		);
		rectangular = false;
		setSize(200, 100);
		change();
	}
	public Boolean getRectangular() {
		return rectangular;
	}
	public void setRectangular(Boolean flag) {
		this.rectangular = flag;
		repaint();
	}
	public void change() {
		color = randomColor();
		repaint();
	}
	private Color randomColor() {
		int r = (int) (255 * Math.random());
		int g = (int) (255 * Math.random());
		int b = (int) (255 * Math.random());
		return new Color(r, g, b);
	}
	public void paint(Graphics g) {
		Dimension d = getSize();
		int h = d.height;
		int w = d.width;
		g.setColor(color);
		if (rectangular) {
			g.fillRect(0, 0, w - 1, h - 1);
		} else {
			g.fillOval(0, 0, w - 1, h - 1);
		}
	}
}

Adding JLabel to JScrollPane makes it easier to display large images


import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
public class ScrollPaneFrame {
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JLabel image = new JLabel(new ImageIcon("A.jpg"));
		frame.getContentPane().add(new JScrollPane(image));
		frame.setSize(300, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}

unicode code was added to JLabel


import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Unicode {
	public static void main(String args[]) {
		UnicodeJFrame unicodeJFrame = new UnicodeJFrame();
		unicodeJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		unicodeJFrame.setSize(350, 250);
		unicodeJFrame.setVisible(true);
	}
}
class UnicodeJFrame extends JFrame {
	public UnicodeJFrame() {
		super("Demonstrating Unicode");
		setLayout(new GridLayout(8, 1));
		JLabel englishJLabel = new JLabel("/u0057/u0065/u006C/u0063" 
		    + "/u006F/u006D/u0065/u0020/u0074/u006F/u0020Unicode/u0021");
		englishJLabel.setToolTipText("This is English");
		add(englishJLabel);
		JLabel chineseJLabel = new JLabel("/u6B22/u8FCE/u4F7F/u7528" + "/u0020/u0020Unicode/u0021");
		chineseJLabel.setToolTipText("This is Traditional Chinese");
		add(chineseJLabel);
		JLabel cyrillicJLabel = new JLabel("/u0414/u043E/u0431/u0440" 
		    + "/u043E/u0020/u043F/u043E/u0436/u0430/u043B/u043E/u0432" 
		    + "/u0430/u0422/u044A/u0020/u0432/u0020Unicode/u0021");
		cyrillicJLabel.setToolTipText("This is Russian");
		add(cyrillicJLabel);
		JLabel frenchJLabel = new JLabel("/u0042/u0069/u0065/u006E/u0076" 
		    + "/u0065/u006E/u0075/u0065/u0020/u0061/u0075/u0020Unicode/u0021");
		frenchJLabel.setToolTipText("This is French");
		add(frenchJLabel);
		JLabel germanJLabel = new JLabel("/u0057/u0069/u006C/u006B/u006F" 
		    + "/u006D/u006D/u0065/u006E/u0020/u007A/u0075/u0020Unicode/u0021");
		germanJLabel.setToolTipText("This is German");
		add(germanJLabel);
		JLabel japaneseJLabel = new JLabel("Unicode/u3078/u3087/u3045" + "/u3053/u305D/u0021");
		japaneseJLabel.setToolTipText("This is Japanese");
		add(japaneseJLabel);
		JLabel portugueseJLabel = new JLabel("/u0053/u00E9/u006A/u0061" 
		    + "/u0020/u0042/u0065/u006D/u0076/u0069/u006E/u0064/u006F/u0020" + "Unicode/u0021");
		portugueseJLabel.setToolTipText("This is Portuguese");
		add(portugueseJLabel);
		JLabel spanishJLabel = new JLabel("/u0042/u0069/u0065/u006E" 
		    + "/u0076/u0065/u006E/u0069/u0064/u0061/u0020/u0061/u0020" + "Unicode/u0021");
		spanishJLabel.setToolTipText("This is Spanish");
		add(spanishJLabel);
	}
}

conclusion

That's the end of this article on Java programming swing component JLabel in detail and using examples that I hope will help. Those who are interested can continue to see this site:

Java Programming swing Circular Button Instance Code

A Brief Discussion on Tag and JLabel Class Constructional Methods

Java Container for Graphical Interface Design (JFrame)

If there is any deficiency, please let me know. Thank you for your support!


Related articles: