Để hiển thị nhiều dòng trong JLabel, dùng thẻ HTML bên trong chuỗi:
JLabel label = new JLabel("<html><strong>Line1<br>Line2</strong></html>", JLabel.LEFT);
Kết quả hiển thị:
Line1 Line2
Ví dụ hoàn chỉnh
import java.awt.Font;
import javax.swing.*;
public class SwingDemo {
public static void main(String args[]) {
JFrame frame = new JFrame("Label Example");
JLabel label = new JLabel("<html><strong>Line1<br>Line2</strong></html>", JLabel.LEFT);
label.setBounds(100, 100, 200, 50);
label.setFont(new Font("Verdana", Font.PLAIN, 13));
frame.add(label);
frame.setSize(500, 300);
frame.setLayout(null);
frame.setVisible(true);
}
}
JLabel sẽ hiển thị hai dòng "Line1" và "Line2" in đậm.