简体   繁体   中英

How do I get my Card (JComponent) to appear fully in my JPanel when I add it to my JFrame?

At the moment, all i see is a thin black line extending from the top left corner of the JFrame screen. I am assuming it is the bottom edge of my card and the rest is blocked from view

When i added the Card straight to the JFrame i could see all of it, so i am confused why i can only see this line (measuring the width of the card) when i add the card to the JPanel in the frame.

Code for JFrame:

public class WarFrame extends JFrame
{
public WarFrame()
{
    setSize(600, 800);
    setTitle("War");
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setBackground(Color.GREEN);
    add(panel);
    panel.add(new Card(Rank.ACE));
}

public static void main(String[] args)
{
    WarFrame game = new WarFrame();
    game.setVisible(true);
}
}

Code for Card:

public class Card extends JComponent
{
private final Rank rank;
private boolean faceUp;
private int x;
private int y;
private final int width;
private final int height;
private final int arcWidth;
private final int arcHeight;

public Card(Rank r)
{
    rank = r;
    faceUp = false;
    x = 0;
    y = 0;
    width = 75;
    height = 100;
    arcWidth = 10;
    arcHeight = 10;
}

public Card(Rank r, int x, int y)
{
    rank = r;
    faceUp = false;
    this.x = x;
    this.y = y;
    width = 75;
    height = 100;
    arcWidth = 10;
    arcHeight = 10;
}

@Override
protected void paintComponent(Graphics g)
{
    Graphics2D pen = (Graphics2D) g;
    //this is the black boarder
    pen.fillRoundRect(x, y, width, height, arcWidth, arcHeight);

    //white card body
    pen.setColor(Color.WHITE);
    pen.fillRoundRect(x + 5, y + 5, width - 10, height - 10, arcWidth, arcHeight);

    if (faceUp)
    {
        //draw the card's symbol
        pen.setFont(pen.getFont().deriveFont(50f));
        pen.setColor(Color.RED);
        if (rank == Rank.TEN)
        {
            //10 has 2 digits, so needs to be shifted a bit
            pen.drawString(rank.getSymbol(), x + 5, y + 65);
        }
        else
        {
            pen.drawString(rank.getSymbol(), x + 20, y + 65);
        }
    }
    else
    {
        //draw a blue rectangle as back of card pic
        pen.setColor(Color.BLUE);
        pen.fillRoundRect(x + 10, y + 10, width - 20, height - 20, arcWidth, arcHeight);
    }
}

I also noticed something interesting about adding the Card straight to the JFrame. The entire card shows up if painted from 0, 0

frame.add(new Card(Rank.ACE, 0, 0));

but if i add it where x > 0,

frame.add(new Card(Rank.ACE, 2, 10));

then the card starts getting cut off on the right side. Somehow when y > 0 the card is painted correctly at a lower part of the screen.

So, any suggestions why A. adding the card to a panel only makes a little line visible and B. when added straight to the frame, why is the card getting cut off only when x > 0?

By default a JPanel uses a FlowLayout which respects the preferred size of any component added to it. When you do custom painting the default preferred size of a JComponent is (0, 0).

You need to override the getPreferredSize() of your Card class to return the proper Dimension for the Card .

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