Question

thank you for taking your time in reading this. I hope it is not confusing, and if any other information is needed, please let me know!

Issue: I am making a Snake like game and I've created a JFrame, and then added the JPanel. Well, if I do not set the size of it in the JFrame it will size to about 100 x 20. When I set the size of it, it stays at the dimensions set. However, if I pack() it, it will shrink back to the 100 x 20 or so, when I have defined the size in the JPanel. I've set the bounds as well, with no luck. I have use FlowLayout(), and BorderLayout() with no success either. Now my game runs correctly and have no issues with it, except for the frame. It will cut off the sides, unless i do some gymnastics, which I do not wish to do.

Objective: I would like to be able to have the Frame size correctly to my width and height specified, and have the panel moved down a bit to add space for text for the game so it is not blocking anything.

Basically I'm confused and flustered that I have spent a few days on this, and I have not been successful with anything so far.

JFrame Class:

public class main extends JFrame {

 /**
  * @param args the command line arguments
  */

 public main(){
     Board game = new Board();

     setLayout(new BorderLayout(0, 50));
     setSize(game.getGameWidth(), game.getGameHeight());
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setLocationRelativeTo(null);
     setTitle("BlockRunner");
     getContentPane().add(game);

     setResizable(false);
     setVisible(true);
 }
}

My Board class:

public class Board extends JPanel implements ActionListener, KeyListener{
 private int width = 600;
 private int height = 400;
 Board(){
     addKeyListener(this);
     setFocusable(true);
     setBackground(Color.LIGHT_GRAY);
     setSize(width, height);
     InitGame();
 }
}

I am willing to try anything someone is willing to put out there for me. Thank you for your time!

EDIT 1: (Due to a user's answer.) using setPerferredSize(), setMinmumSize() and setMaximumSize() does not affect the outcome of this.

Was it helpful?

Solution

You need to use a combination of getPreferred/Minimum/MaximumSize and a layout that will actually respect it.

Here, I've used a GridBagLayout set up so that the board will always stay the same size, but the cell will use the maximum amount of same.

If you want to keep the text at the bottom of the board (instead of the frame), remove the weighty constraint (and possibly the weightx) as well

enter image description hereenter image description here

public class TestBoardGame {

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

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

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.weightx = 1;
                gbc.weighty = 1;
                frame.add(new BoardGamePane(), gbc);

                gbc.gridx = 0;
                gbc.gridy = 1;
                gbc.weightx = 1;
                gbc.fill= GridBagConstraints.HORIZONTAL;
                JLabel text = new JLabel("Area for text");
                text.setHorizontalAlignment(JLabel.CENTER);
                frame.add(text, gbc);

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class BoardGamePane extends JPanel {

        public BoardGamePane() {

            setBorder(new LineBorder(Color.RED));
            setBackground(Color.ORANGE);

        }

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

        @Override
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

        @Override
        public Dimension getMaximumSize() {
            return getPreferredSize();
        }

    }    
}

OTHER TIPS

Assuming that Board is used principally for drawing, you can override getPreferredSize() to return a suitable Dimension. When you pack() the enclosing Window, it will adopt the chosen size. See this Q&A for more, and avoid this pitfall.

I know you say that using setPreferredSize() isn't affecting the outcome, but two others and myself seem to think it is what should fix it. Can you try the following:

In main change this:

setSize(game.getGameWidth(), game.getGameHeight());

To:

setPreferredSize(new Dimension(game.getGameWidth(), game.getGameHeight()));

Then add pack() and see if the outcome has changed. Theoretically this should fix the issue unless something else is going on that we can't see.

My preferred way of using swing is to have inner components set their preferred size, and have the JFrame pack.

So in total you need to:

  • remove setSize in JFrame (or change to setPreferredSize)
  • change setSize in JPanel to setPreferredSize.
  • call pack in JFrame after everything is done.

See if the code below does what you want.

public class Main extends JFrame {

    public Main() {
        Board game = new Board();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        {
            Container contentPane = getContentPane();
            contentPane.setLayout(new BorderLayout(0, 50));
            JLabel label = new JLabel("hello world!");
            contentPane.add(label, BorderLayout.NORTH);
            contentPane.add(game, BorderLayout.CENTER);
        }
        pack();

        setResizable(false);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Main();
            }
        });
    }
}

and

public class Board extends JPanel {
    private int width = 600;
    private int height = 400;

    Board() {
        setFocusable(true);
        setBackground(Color.LIGHT_GRAY);
        setPreferredSize(new Dimension(width, height));
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top