Reputation: 1325
I am trying to set a couple newlines in a java swing window, but for some reason everything is printed on one line.
This is my code:
private JLabel lblOutput;
guess = 79;
numberOfTries = 5;
message = guess + " is correct. " + "\n" + "Let's play again! " + "\n" + "And it only took you " + numberOfTries + " number of tries!";
lblOutput.setText(message);
The output of the above is:
79 is correct. Let's play again! And it only took you 5 number of tries!
What am I doing wrong?
Upvotes: 0
Views: 54
Reputation: 81
If lblOutput is a JLabel, it only accepts a single line of text.
You can try using HTML formatting to get around it.
Upvotes: 3
Reputation: 324098
A label is designed to only display a single line of text.
However, you can use simple HTML to split the text on multiple lines:
label.setText("<html>line1<br>line2</html>");
Upvotes: 3