0

I've created a basic program with java that creates a five card poker hand and then displays them in a JFrame. I'm essentially displaying five images in a frame, but instead of displaying in the positions that I think they should be in, they look like

enter image description here

Any help would be appreciated, thanks!

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.*;
import java.awt.Color;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import java.awt.Point;
import java.net.URL;
import javax.smartcardio.Card;

public class PokerGUI extends JFrame {

    private static JPanel contentPane;
    private PokerBoard board;
    private static JLabel[] displayCards;
    private static Point[] cardCoords;
    private static final int LAYOUT_WIDTH_INC = 100;
    private static final int LAYOUT_HEIGHT_INC = 125;
    private static final int LAYOUT_TOP = 30;
    private static final int LAYOUT_LEFT = 30;
    private static boolean[] selections;

    public PokerGUI(PokerBoard gameBoard) {
        board = gameBoard;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 530, 252);
        contentPane = new JPanel();
        setResizable(false);
        setTitle("SUPER COOL POKER GAME!!!!");
        contentPane.setBackground(Color.DARK_GRAY);
        contentPane.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, new Color(0, 255, 0), new Color(0, 255, 0), new Color(0, 128, 0), new Color(0, 128, 0)));
        contentPane.setToolTipText("Super Cool Poker!!");
        contentPane.setLayout(new BorderLayout(0, 0));

        selections = new boolean[5];
        displayCards = new JLabel[5];
        Point one = new Point(30, 30);
        Point two = new Point(130, 30);
        Point three = new Point(230, 30);
        Point four = new Point(330, 30);
        Point five = new Point(430, 30);

        cardCoords = new Point[]{one, two, three, four, five};
        init();

        for (int k = 0; k < 5; k++) {
            String cardImageFileName = imageFileName(board.cardAt(k), selections[k]);
            URL imageURL = getClass().getResource(cardImageFileName);
            if (imageURL != null) {
                ImageIcon icon = new ImageIcon(imageURL);
                displayCards[k].setIcon(icon);
                displayCards[k].setVisible(true);
            } else {
                throw new RuntimeException("Card image not found: \"" + cardImageFileName + "\"");
            }
        }

        setContentPane(contentPane);
    }

    private static void init() {

        for (int k = 0; k < 5; k++) {
            displayCards[k] = new JLabel();
            contentPane.add(displayCards[k]);
            displayCards[k].setBounds(cardCoords[k].x, cardCoords[k].y, 73, 97);
            //displayCards[k].addMouseListener(new MyMouseListener());
            selections[k] = false;
        }
    }

    private String imageFileName(Card c, boolean isSelected) {
        String str = "cards/";
        if (c == null) {
            return "cards/back1.GIF";
        }
        str += c.rank() + c.suit();
        if (isSelected) {
            str += "S";
        }
        str += ".GIF";
        return str;
    }

}

The other classes shouldn't really be important because my main issue is just getting the five images to display in the correct places.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 3
    And what is "wrong" with the way the cards are laid out? – MadProgrammer May 22 '15 at 00:52
  • Do you want them in a grid? – PM 77-1 May 22 '15 at 00:54
  • I'm trying to display them in a line, at the points specified in cardCoords[]. They are all in the correct place except for the jack of diamonds, which is displaying in the bottom left instead of far right. – Finn Voorhees May 22 '15 at 01:00
  • Your `contentPane` is using a `BorderLayout`, so it is (trying to) control the layout of the last component you added to it (generally ignoring the rest). You might consider using a `GridBagLayout` or maybe even a `JLayeredPane` – MadProgrammer May 22 '15 at 01:02
  • Ah thanks. I think I'll just comment it out for now. I'm pretty new to JFrames and don't even know what a border layout is. – Finn Voorhees May 22 '15 at 01:06
  • @FinnVoorhees - Research what `contentPane.setLayout(new BorderLayout(0, 0));` in your code actually does. – PM 77-1 May 22 '15 at 17:44

1 Answers1

1
contentPane.setLayout(new BorderLayout(0, 0));

First you set the layout to a BorderLayout which only accepts a single component in the CENTER.

contentPane.add(displayCards[k]);

Then you add all the cards to the CENTER.

displayCards[k].setBounds(cardCoords[k].x, cardCoords[k].y, 73, 97);

Then you ignore the layout manager completely and try to set the Bounds of each card.

However, when the frame is displayed, the last card added will have its bounds set by the layout manager, which will override your bounds.

Check out my answer in this posting: Why does the first panel added to a frame disappear? for more information on why this happens.

I'm trying to display them in a line

So the solution is to use a FlowLayout and let the layout manager determine the location of each card. Don't attempt to set the bounds of each card manually.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288