0

This may seem like a stupid question but I'm unable to do it; I use a scanner to read a file then using a while loop I input the text into a string which I then put onto a JLabel. However, the text is displayed in one very long horizontal line, how to I make it so that all the text appear normally like paragraphs as in the original text file? The code:

class howToPlay implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
    JFrame htp = new JFrame();
    htp.setSize(300, 100);
    htp.setLocationRelativeTo(null);
    Scanner fileStream = null;
    try {
        fileStream = new Scanner(new File("text/howtoplay.txt"));
    } catch (FileNotFoundException e1) {
        System.out.println("File not found");
        e1.printStackTrace();
    }
    String file = "";
    while(fileStream.hasNextLine())file += fileStream.nextLine();
    JLabel howToPlay = new JLabel(file);
    htp.add(howToPlay);
    htp.setVisible(true);

}

}

This is the text in the file, which is rules of how to play a game:

The Object of the Game is to move your pieces until they are all in one connected group. Diagonals are considered to be connected. However, there are certain rules that need be followed:

-White moves first -Each turn, the player to move moves one of his pieces, in a straight line, exactly as many squares as there are pieces of -either color anywhere along the line of movement. (These are the Lines of Action). -You may jump over your own pieces. -You may not jump over your opponents pieces, but you can capture them by landing on them. -If one player is reduced by captures to a single piece, that is a win for the captured player. -If a move simultaneously creates a win for both the player moving and the opponent, the player moving wins. There are actually quite a few Unusual endgames which are at least theoretically possible.

Any help is appreciated.

mKorbel
  • 108,320
  • 17
  • 126
  • 296
mhalabi
  • 31
  • 6

2 Answers2

0

Maybe try out this :)

Looks weird, but work fine for me.

Community
  • 1
  • 1
Syry
  • 13
  • 4
0

You can use HTML formatting: prefix .setText() with:

<html>

and you will be able to use HTML code in your JLabel. Example:

label.setText("<html>First line<br>Second line");
Mac70
  • 310
  • 5
  • 14