-1

I have spent the last few hours of the past two days trying to figure out why I can't get my GUI to work right. Here is where I come to all of you to see if you can help me at all.

Now what i'm trying to do is a Text Based game, simple right? Well I want to use buttons lined up on the left side of the screen, with your health and all that fun stuff on the bottom, with the main meat of the game, the TEXT, in the center, but no matter what i did i couldn't get it to fit right, then i stumbled upon pack() so i tried it, and yea...it did what it was supposed to do, sadly right now i have ONE WORD and the screen is tiny, any ideas as to how i could make the screen bigger, and remain big?

Going to edit this and throw you bits and peices of my code, including for the textarea (please note i have been using java for a whopping two days while not at work)

  JTextArea textarea = new JTextArea("example");

    frame.add(textarea);
    //frame.pack();

And here i have pack commented out because i'm playing around with it.

Anything else you guys want to take a peek at?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Kage
  • 23
  • 1
  • 6

3 Answers3

1

There are so many things that could be going wrong, without more code it's difficult to pin point an exact problem...

A few things jump to mind...

  • Use appropriate layout managers, even compound layout managers
  • Provide additional information to the JTextArea to allow it to define a usable space, such as rows and columns
  • Add the JTextArea to a JScrollPane to ensure that it doesn't suddenly occupy more space then is actually available

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout21 {

    public static void main(String[] args) {
        new TestLayout21();
    }

    public TestLayout21() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());

            JPanel actions = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill= GridBagConstraints.HORIZONTAL;

            actions.add(new JButton("Walk"), gbc);
            actions.add(new JButton("Run"), gbc);
            actions.add(new JButton("Jump"), gbc);
            actions.add(new JButton("Eat"), gbc);
            actions.add(new JButton("Drink"), gbc);

            add(actions, BorderLayout.WEST);
            add(new JProgressBar(), BorderLayout.SOUTH);

            JTextArea textArea = new JTextArea(20, 40);
            add(new JScrollPane(textArea));

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You could try setting the preferred size on your objects or the panel/frames themselves:

myPanel.preferredSize(new Dimension(1000,500));

or

myFrame.preferredSize(new Dimension(1000,500));

or

myTextArea.preferredSize(new Dimension(300,200));
lgraham076
  • 81
  • 8
  • 1
    No. Use `JTextArea#setRows/Columns` to better effect the preferred size. If at all possible, avoid using `setPreferredSize`. If the OP isn't using any layout managers, then this will have no effect anyway. – MadProgrammer Mar 20 '13 at 07:23
  • 2
    actually, there isn't any `component.preferredSize(Dimension)` - please be precise (even if the answer is as suboptimal as this, for reasons to _not_ use `setPreferredSize(Dimension)` [see a recent QA](http://stackoverflow.com/a/7229519/203657)). – kleopatra Mar 20 '13 at 08:50
0

Pack will indeed just make your frame or panel only as big as needed to fit the components who are in it. Try preferredSize() method, and maybe post some code if it still isn't working.

Kuurde
  • 887
  • 2
  • 8
  • 24