Milica P
Milica P

Reputation: 37

Trying to make a JLayeredPane and it's contents visible with no luck, no sure whats wrong

Trying to make a board game for my Java homework, however I'm having trouble getting the contents of my JLayeredPane to appear - only the border appears. Also, everything appears fine when I use a default layout, but those ignore layers and squish em into squares. It doesn't appear that setting x and y in grid.setBounds() does anything either - so I've just left them as 0 for now. The variable names are a bit funky - grid is just one square of the grid, and boardGUI is the whole board/collection of grids. I'm trying to place a piece (done using a label) on top of grid.

I'll be showing this with almost pseudo-code so my point is slightly easier/quicker to get across. Yes, I've looked through the oracle swing tutorial on this.

I'll show of this in pseudocode with some explainations - this is homework so I don't want to show anything that isn't a basic/common part of drawing GUIs, hope you understand. I believe these are the main things I need to set in order to have my JlayeredPane appear:

setSquare(boardArray){
    JLayeredPane grid = new JLayeredPane();
    grid.setBorder(new LineBorder(new Color(0,70,0)));
    grid.setPreferredSize(new Dimension(width, width));//width is set to 50
    grid.setMinimumSize(new Dimension(width, width));
    grid.setMaximumSize(new Dimension(width, width));
    grid.setBounds(0, 0, width, width);

    for every item in the array{
        if theres a black piece in the array:
            drawPiece(grid, blackFill, whiteBorder);
        else if theres a white piece in the array:
            drawPiece(grid, whiteFill, blackBorder);
    }

    grid.setVisible(true);
    boardGUI.add(grid);
}

drawPiece(JLayeredPane grid, Color fill, Color border){
    piece = new CircleLabel(size, fill, border); //custom method that creates a label shaped like a circle - it inherits from JLabel
    piece.setVisible(true);
    piece.setLayer(piece, JLayeredPane.PALETTE_LAYER);
    grid.add(piece, JLayeredPane.PALETTE_LAYER);
}

This is using the code above:

This is using the code above

This is what appears if I use GridLayout and don't do the last 5 lines shown. This is what appears if I use GridLayout and don't do the last 5 lines shown.

Also, it compiles with no errors or warnings.

Upvotes: 1

Views: 48

Answers (0)

Related Questions