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);
}
}