问题
I have a label and when setting the text I want to change line . For instance
String str="first line \n Secondline ";
JLabel label.setText(str);
I tried the above code but it does not work..How do I change line in a string ?
回答1:
Swing does not uses classical line breaks. Instead, for a reason I never fully understood, it uses HTML 3.2. As a consequence, to have your text correctly displayed, yopu have to rely upon HTML content :
String str= "<html>First line<br>Second line</html>"
However, if you really want to, there are other possibilities ...
回答2:
Use this string
String str= "<html>First line<br>Second line</html>"
来源:https://stackoverflow.com/questions/4546204/how-do-we-changeline-in-a-label