[JavaSwing] JLabel

Content
Create simple JLabel
Put color, background color for JLabel

JLabel is often used to display text or image to create guidelines, instructions on the user interface.
JLabel
In the picture above using 4 JLabel user to correctly enter the required information.

Example 1: Create simple JLabel

package nguyenvanquan7826.JLabel;

import java.awt.GridLayout;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MyJLabel extends JFrame {
	public MyJLabel() {
		// create frame
		setLayout(new GridLayout(1, 3, 5, 5));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// create image
		Icon icon = new ImageIcon(getClass().getResource("7826.png"));

		// create three JLabel
		JLabel lb1 = new JLabel("label text only");
		JLabel lb2 = new JLabel(icon);
		JLabel lb3 = new JLabel("icon and text", icon, JLabel.CENTER);
		lb3.setVerticalTextPosition(JLabel.BOTTOM);
		lb3.setHorizontalTextPosition(JLabel.CENTER);

		// add three label to frame
		add(lb1);
		add(lb2);
		add(lb3);

		// display frame
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}

	public static void main(String[] args) {
		new MyJLabel();
	}
}

The results have been 3 JLabel as follows (Note 7826.png file is placed in the same package as shown)

JLabel

In this example, we note the following command:
– setLayout(new GridLayout(1, 3, 5, 5)); Orders placed GridLayout for JFrame with 1 row, 3 post, columns and rows are separated by 5px. Layout Temporary understand the layout of objects for JFrame, if no layout, the object will be overlap.
– Icon icon = new ImageIcon(); Read command file photo imaging for JLabel.
– Followed by 3 commands 3 JLabel form. JLabel all 6 form initialization:
+/ JLabel(): Create a JLabel instance with no image and an empty string
+/ JLabel(Icon image): Create an instance of JLabel assign a picture
+/ JLabel(Icon image, int horizontalAlignment): Create an instance of JLabel specify an image and horizontal alignment
+/ JLabel(String text): Create an instance of the specified text JLabel
+/ JLabel(String text, Icon icon, int horizontalAlignment): Create an instance of the specified text JLabel, image và horizontal alignment
+/ JLabel(String text, int horizontalAlignment): Create an instance of the specified text JLabel, và horizontal alignment.

In LB3 we have 2 jaw setVerticalTextPosition and setHorizontalTextPosition to set the position of the text vertically (BOTTOM) and horizontally (CENTER). For the same LB1 JLabel containing only text if desired alignment (left, right, between, …) we use the method setHorizontalAlignment(int alignment).

Example 2: Put color, background color for JLabel

package nguyenvanquan7826.JLabel;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class MyJlabelWithColor extends JFrame {
	public MyJlabelWithColor() {
		setLayout(new GridLayout(1, 2, 5, 5));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(400, 200);

		// create JLabel have text color red and background color green
		JLabel lb;
		lb = createJLabel("JLabel 1", Color.red, Color.green);
		add(lb);
		// create JLabel have text color blue and background color yellow
		lb = createJLabel("JLabel 2", Color.blue, Color.yellow);
		add(lb);
		
		// display JFrame
		setLocationRelativeTo(null);
		setVisible(true);
	}

	private JLabel createJLabel(String text, Color textColor,
			Color backgroundColor) {
		JLabel lb = new JLabel(text);
		// set align
		lb.setHorizontalAlignment(JLabel.CENTER);
		// set color
		lb.setForeground(textColor);
		// set background color
		lb.setOpaque(true);
		lb.setBackground(backgroundColor);

		return lb;
	}

	public static void main(String[] args) {
		new MyJlabelWithColor();
	}
}

color in JLabel

In this example we create a function createJLabel returns 1 JLabel with the arguments passed to turn the text, its color and color brackground. It is noteworthy that in order to accommodate the background, we need to put Opaque JLabel is true of (The default is false).

Refer to: class JLabel