2

So I want my imported text file called "Instructions.txt" to be added to a JFrame for the player to see if he/she chooses to look at instructions. I can get one word to show up on the very left of the screen, but nothing else. Also, if I resize the window by dragging one of the sides, multiple words show up on top of that first word on the left. Any ideas?

 //Battleship.java

import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
import java.io.*;
import javax.swing.JOptionPane;

public class Battleship
{
 public static void main (String[] args) throws IOException
{
String name = JOptionPane.showInputDialog("What is your name?");

    String answer = JOptionPane.showInputDialog("Welcome to Battleship, "+name+". Would you         like to see a set of instructions?");

if(answer.equals ("yes") || answer.equals ("Yes"))
{
TextFrame textframe = new TextFrame();
  textframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  Scanner scan = new Scanner (new File("Instructions.txt"));

  while (scan.hasNext())
  {
    textframe.displayText(scan.next());

  }

JFrame frame = new JFrame("Battleship");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Buttons b = new Buttons();

JPanel panel = new JPanel();
panel.add(b);
panel.setBackground(Color.blue);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

frame.getContentPane().add (panel);
frame.pack();
frame.setVisible(true);
}

else
{    
JFrame frame = new JFrame("Battleship");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Buttons b = new Buttons();

JPanel panel = new JPanel();
panel.add(b);
panel.setBackground(Color.blue);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

frame.getContentPane().add (panel);
frame.pack();
frame.setVisible(true);
}

 }

 }

 //TextFrame.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLabel;

 public class TextFrame extends JFrame 
 {

    public TextFrame() 
{

    setTitle("Instructions");
    setSize(400,500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}

public void displayText(String text) 
{       
JLabel Text = new JLabel(text);          
add(Text);                               
 }
 }
Mdomin45
  • 469
  • 1
  • 6
  • 14

4 Answers4

0

There are components that will handle this for you. Start here:

http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html

Dodd10x
  • 3,344
  • 1
  • 18
  • 27
  • I do not understand why this was downvoted. Though this is just the general link to the official docs and no *direct* coding help, these docs are relevant and helpful to answer the question, meeting SO rules. – questionto42 Jul 11 '20 at 17:42
0

Maybe you should just read the whole "Instructions.txt" file before calling a method to display every token of it. Declare a variable, write the contents of the file into it, and then display it. As the previous answer suggests, JLabel isn't the best component to display a bunch of text.

Igorsvee
  • 4,101
  • 1
  • 25
  • 21
0

At the moment your are adding a new JLabel for each line of the text file. All these JLabel are added to the center region of the frame, replacing all the components you have added before. Therefore the only word you see is the last word from the file. You can fix that by using the following code:

Scanner scan = new Scanner (new File("Instructions.txt"));
StringBuilder sb = new StringBuilder();

while (scan.hasNext()) {
    sb.append(scan.next());
}

textframe.displayText(sb.toString());

To show long texts in a JLabel I would also wrap the text in html tags:

public void displayText(String text) {       
    JLabel lblText = new JLabel("<html><body>" + text + "</body></html>");          
    add(lblText);                             
}

Moreover I would suggest to read your file using a BufferedReader. To be honest I have never used a Scanner to read a text file and I am not sure, if this is good practice. An example can be found here: Reading and displaying data from a .txt file

Community
  • 1
  • 1
trylimits
  • 2,575
  • 1
  • 22
  • 32
0

I updated your code with a JTextArea. I had it all in one file so I removed some imports. You should fix those items.

import java.util.Scanner;
import java.io.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Battleship {

    public static void main(String[] args) throws IOException {

        String name = JOptionPane.showInputDialog("What is your name?");

        String answer = JOptionPane.showInputDialog("Welcome to Battleship, " + name + ". Would you         like to see a set of instructions?");

        if (answer.equals("yes") || answer.equals("Yes")) {
            TextFrame textframe = new TextFrame();
            JTextArea jta = new JTextArea();
            jta.setEditable(false);
            jta.setLineWrap(true);
            jta.setWrapStyleWord(true);
            textframe.add(jta);
            textframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Scanner scan = new Scanner(new File("Instructions.txt"));

            StringBuilder sb = new StringBuilder();
            while (scan.hasNextLine()) {
                String line = scan.nextLine();
                jta.append(line);

            }

            JFrame frame = new JFrame("Battleship");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

           // Buttons b = new Buttons();

            JPanel panel = new JPanel();
           // panel.add(b);
            panel.setBackground(Color.blue);
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

            frame.getContentPane().add(panel);
            frame.pack();
            frame.setVisible(true);
        } else {
            JFrame frame = new JFrame("Battleship");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //Buttons b = new Buttons();

            JPanel panel = new JPanel();
          //  panel.add(b);
            panel.setBackground(Color.blue);
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

            frame.getContentPane().add(panel);
            frame.pack();
            frame.setVisible(true);
        }

    }

}

  class TextFrame extends JFrame {

    public TextFrame() {

        setTitle("Instructions");
        setSize(400, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void displayText(String text) {
        JLabel Text = new JLabel(text);
        add(Text);
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720