How to create line breaks in content using labels.
In Java, you can use HTML tags to implement line breaks in a label. Here is an example code:
import javax.swing.JFrame; import javax.swing.JLabel; public class Main { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("第一行
第二行"); frame.add(label); frame.setVisible(true); } }
The above code creates a simple GUI window and adds a label with line breaks in the window. We used the HTML `
` tag to indicate line breaks, placing the text that needs to break between `
` and `
` tags.
When running the above code, a window with two lines of text will be displayed, with each line on a separate line.