Ok I am making a user interface for Battleship and want a Frame to appear on the side with the title and instructions of the game in a disabled JTextArea. With the code I have now I can get the JFrame to open blank. The JTextArea only appears if I resize the window. I also can't keep the JTextArea within the bounds of the JFrame/ JPanel. Any help? In this code there is a JTextField, But I have tried Labels and Areas, with the same result.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleship;

import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import javax.swing.*;

/**
 *
 * @author Xaver
 */
public class Instructions extends JFrame
{
    private JPanel myPanel;

    public Instructions()
    {   setTitle("Instructions");
        setLayout(new GridBagLayout());
    setSize(700, 450); // default size is 0,0
    setLocation(700, 0); // default is 0,0 (top left corner)
        //setResizable(false);
        setVisible(true);        
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setUpPanel();   
    }

    public final void setUpPanel()
    {   myPanel = new JPanel();
        String myString = "Welcome to BattleShip"
        +"This game is a turn based strategy game that "
        +"the basic need is to sink the enemy ships before "
        +"the enemy sinks yours, you have to input x and \n"
        +"y coordinates within the range. On the enemy side \n"
        +"~ means misses and * means hits. Happy Hunting!\n";
        JTextField jTC= new JTextField(100);
        //myPanel.setSize(700,400);
        jTC.setText(myString);
        //jTC.setBounds(750, 100, 100, 100);
        jTC.setEnabled(false);
        repaint();
        myPanel.add(jTC);
        add(myPanel);


    }

}

Edited 3 Years Ago by Nandomo

I have read it. you have problems with your JTextArea, but you don't have one in your code.
so: what JTextArea are we talking about?

I notice you make your frame visible before you finish setting it up. That's a strange decision. Why allow the user to see it before it is ready to be seen? And making it visible before you set the default close operation is asking for there to be a moment when the user could click the close button without triggering your chosen operation.

You haven't included the place where the Instructions constructor is called, so just to be safe I'll remind you that you can only safely interact with Swing components from the event dispatch thread. For example, you may cause unpredictable problems if you call the constructor from a main method.

Edited 3 Years Ago by bguild

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleship;

import java.awt.FlowLayout;
import javax.swing.*;

/**
 *
 * @author Xaver
 */
public class Instructions extends JFrame
{
    private JPanel myPanel;

    public Instructions()
    {   setTitle("Instructions");
        //setLayout(new FlowLayout());
    setSize(700, 450); // default size is 0,0
    setLocation(700, 0); // default is 0,0 (top left corner)

        setUpPanel();   
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public final void setUpPanel()
    {   myPanel = new JPanel();
        String myString = "<html><p>Welcome to BattleShip"
        +"This game is a turn based strategy game that "
        +"the basic need is to sink the enemy ships before "
        +"the enemy sinks yours, you have to input x and "
        +"y coordinates within the range. On the enemy side "
        +"~ means misses and * means hits. Happy Hunting!</p></html>";
        JTextArea jTC= new JTextArea(myString);
        jTC.setWrapStyleWord(true);
        myPanel.setSize(400, 400);
        add(myPanel);

        myPanel.add(jTC);

    }

}



package battleship;


import javax.swing.JOptionPane;

public class BattleShip {

    /********************************************************
     * 
     *                     MAIN
     * Purpose: Calls most methods, sets Mode, checks for
     *          winner and prints linked lists with hits
     * 
     * @param args
     * 
     *******************************************************/
    public static void main(String[] args) 
    {
        Addons a = new Addons();
        Computer c = new Computer();
        Human h = new Human();
        LinkedList list = new LinkedList();
        LinkedList list2 = new LinkedList();
        Board b = new Board();
        Sound s = new Sound();
        String input = "";
        GuiBoard guiHuman = new GuiBoard();
        GuiBoard guiComputer = new GuiBoard();
        Instructions i = new Instructions();
        i.setVisible(true);

        while(a.intro())
        {
            do
            {
                try
                {
                    input = JOptionPane.showInputDialog(
                        "What Mode do you want to play? E, M or H?").toUpperCase();
                     switch(input)
                    {
                    case "E":
                        b = new Board("easy");
                        b.prepareBoard();
                        guiHuman = new GuiBoard(b.getBoard(), 1);
                        guiComputer = new GuiBoard(b.getBoard(), 3);
                        break;
                    case "M": 
                        b = new Board("medium");
                        b.prepareBoard();
                        guiHuman = new GuiBoard(b.getBoard(), 1);
                        guiComputer = new GuiBoard(b.getBoard(), 3);
                        break;
                    case "H":
                        b = new Board("hard");
                        b.prepareBoard();
                        guiHuman = new GuiBoard(b.getBoard(), 1);
                        guiComputer = new GuiBoard(b.getBoard(), 3);
                        break;
                    default:
                        break;

                    }
                }
                catch (NullPointerException npe)
                {
                    System.exit(0);
                }
            }while(a.checkMode(input));


            s.playIntro("start");
            a.countDown(5, s);

        do
        {
            // display


            guiComputer.setVisible(false);
            guiComputer = new GuiBoard(b.getBoard(), 2);
            a.sleep(3);
            if(a.checkWinner() == false) 
            {
                break;
            }
            c.play();
            a.checkWinner();
            guiHuman.setVisible(false);
            guiHuman = new GuiBoard(b.getBoard(), 1);

        }while(b.nextRound() && a.checkWinner());

        s.playIntro("stop");
        if(!list.isEmpty()) 
        {
            System.out.println("Human:");
            list.printLinkedList();
        }
        if(!list2.isEmpty()) 
        {
            System.out.println("Computer:");
            list2.printLinkedList();
        }



        }
    }
}

Ok I made the visible after the call. Yet I can't get the TextArea to stay within the Panel. I've tried setting size for everything... Numbering rows and columns in the TextArea call... Nothing.

You probably have too much text for your JTextArea to paint on one line. I recommend that you either put some newlines into your text or else turn on line wrapping.

If you want the HTML to be displayed properly you should consider using a javax.swing.JLabel instead of the JTextArea, and look at: How to Use HTML in Swing Components. If you want it to be editable you should look into javax.swing.JTextPane and Using Text Components.

This question has already been answered. Start a new discussion instead.