简体   繁体   中英

GridLayout Panel adding not considering preferreed size

I'm currently coding a java project and I need a JPanel named board to contain some Tiles that are themselves JPanel, I add them to a layout of appropriate size, but when I add everything with buttons in a grouplayout in a JFrame, only the first Tile is at an appropriate size, and others have ridiculous size like being 1p width and 50p height or 1*1p.

I don't feel like I miss anything, everything is added in the JFrame and added to correct layout, with correct number of rows and lines, and sizes are set to be 50*50p in the Tile class. Here are some code snippets containing the graphic settings, especially the constructors:

Tile.java :

class Case extends JPanel
{

private int _color;
private boolean _star;
private Case _casePere;
private ArrayList<Case> _fils;
private int _x,_y;

public Case(int x, int y)
{
    _color = 0;
    _star = false;
    _casePere = null;
    _fils = new ArrayList<Case>();
    _x = x;
    _y = y;
    setPreferredSize(new Dimension(50,50));
    setMinimumSize(new Dimension(50,50));
    setBackground(Color.WHITE);
}

Board.java :

class Board extends JPanel{

private Case[][] _grid;
private Case[] _starsp1;
private Case[] _starsp2;

// constructeur
public Board(int nbStars, int length){



    _grid = new Case[length][length];
    _starsp1 = new Case[nbStars];
    _starsp2 = new Case[nbStars];

    //graphisme
    Dimension d = new Dimension(50*length, length*50);

    setBackground(Color.BLACK);
    setPreferredSize(d);


    GridLayout layout = new GridLayout(Constante.length, Constante.length,2 ,2);
    setLayout(layout);

    for(int y=0; y<length; ++y)
    {
        for(int x=0; x<length; ++x)
        {

            _grid[x][y] = new Case(x,y);
            add(_grid[x][y]);
        }
    }

if you want, I can add some snippets of the Window class, but beside the Board, other components don't hav any issues, and it's mainly adding and grouping some components. Here's a screenshot of the output so you can see how the Board's drawing behaves

Edit : I was overriding getX and getY in my Case class overriding JPanel's one, kinda dumb issue again, thanks for the answers

https://i.stack.imgur.com/BAosv.png

setPreferredSize(d);

Don't set the preferred size of the board. The layout manager of the board will determine the preferred size based on the number of components added to the grid and the size of the largest component added.

Note, (based on the line below) you want a spacing of 2 pixels between each component which your calculation doesn't include. So let the layout manager do its job.

GridLayout layout = new GridLayout(Constante.length, Constante.length,2 ,2);

We don't know what Constante.length is. You pass in a length variable to your class so use that variable.

Also, why does your Case class have so many instance variables? Those variables are never used in the posted code. So maybe you have other methods that are causing problems with the layout. For example don't override getX() or getY() those methods are used by the layout manager.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM