[JavaSwing] JLabelの

Nội dung
Tạo JLabel đơn giản
Đặt màu, màu nền cho JLabel

JLabel thường được dùng để hiển thị text hoặc hình ảnh để tạo các chỉ dẫn, hướng dẫn trên giao diện người dùng.
JLabelの
Trong hình trên có sử dụng 4 JLabel để hướng dẫn người dùng nhập chính xác các thông tin cần thiết.

Ví dụ 1: Tạo các JLabel đơn giản

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();
	}
}

Kết quả ta được 3 JLabel như sau (Chú ý file 7826.png được đặt trong cùng package như hình bên)

JLabelの

Trong ví dụ trên chúng ta chú ý một số lệnh sau:
– setLayoutの(新しいGridLayoutの(1, 3, 5, 5)); Lệnh đặt GridLayoutの cho JFrame với 1 hàng, 3 cột, các cột và hàng cách nhau là 5px. レイアウト tạm thời hiểu như cách bố trí các đối tượng cho Jframe, nếu không có Layout thì các đối tượng sẽ bị đè lên nhau.
– アイコンアイコン=新しいイメージアイコン(); Lệnh đọc file ảnh tạo ảnh cho JLabel.
Tiếp theo là 3 lệnh tạo 3 dạng JLabel. JLabel có tất cả 6 dạng khởi tạo:
+/ JLabelの(): Tạo một thể hiện JLabel không có hình ảnh và một chuỗi rỗng
+/ JLabelの(アイコン画像): Tạo một thể hiện JLabel chỉ định một hình ảnh
+/ JLabelの(アイコン画像, INTたHorizo​​ntalAlignment): Tạo một thể hiện JLabel chỉ định một hình ảnh và horizontal alignment
+/ JLabelの(文字列のテキスト): Tạo một thể hiện JLabel chỉ định text
+/ JLabelの(文字列のテキスト, アイコンアイコン, INTたHorizo​​ntalAlignment): Tạo một thể hiện JLabel chỉ định text, image và horizontal alignment
+/ JLabelの(文字列のテキスト, INTたHorizo​​ntalAlignment): Tạo một thể hiện JLabel chỉ định text, và horizontal alignment.

Trong lb3 chúng ta có 2 hàm setVerticalTextPosition setHorizo​​ntalTextPosition để đặt vị trí của text theo chiều dọc (BOTTOM) và theo chiều ngang (CENTER). Đối với JLabel chỉ chứa text giống lb1 nếu muốn căn lề (trái, phải, giữa, …) ta dùng phương thức setHorizo​​ntalAlignment(int型のアライメント).

Ví dụ 2: Đặt màu, màu nền cho 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();
	}
}

JLabelの色

Trong ví dụ này ta tạo hàm createJLabel trả về 1 JLabel với các đối số truyền vào lần lượt là text, color và brackground color của nó. Đáng lưu ý là để đặt được màu nền thì chúng ta cần đặt 不透明な của JLabel là true (mặc định là false).

Tham khảo thêm: クラスのJLabel