I can't figure out how to display the ImageIcons after I get three random cards. I tried to add the .png to the random number and that didn't work. I would appreciate your help.

import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class Exercise139 
{
    public static void main(String[] args)
    {
        ImageIcon aceSpades = new ImageIcon("card/card/1.png");
        ImageIcon twoSpades = new ImageIcon("card/card/2.png");
        ImageIcon threeSpades = new ImageIcon("card/card/3.png");
        ImageIcon fourSpades = new ImageIcon("card/card/4.png");
        ImageIcon fiveSpades = new ImageIcon("card/card/5.png");
        ImageIcon sixSpades = new ImageIcon("card/card/6.png");
        ImageIcon sevenSpades = new ImageIcon("card/card/7.png");
        ImageIcon eightSpades = new ImageIcon("card/card/8.png");
        ImageIcon nineSpades = new ImageIcon("card/card/9.png");
        ImageIcon tenSpades = new ImageIcon("card/card/10.png");
        ImageIcon jackSpades = new ImageIcon("card/card/11.png");
        ImageIcon queenSpades = new ImageIcon("card/card/12.png");
        ImageIcon kingSpades = new ImageIcon("card/card/13.png");
        ImageIcon aceHearts = new ImageIcon("card/card/14.png");
        ImageIcon twoHearts = new ImageIcon("card/card/15.png");
        ImageIcon threeHearts = new ImageIcon("card/card/16.png");
        ImageIcon fourHearts = new ImageIcon("card/card/17.png");
        ImageIcon fiveHearts = new ImageIcon("card/card/18.png");
        ImageIcon sixHearts = new ImageIcon("card/card/19.png");
        ImageIcon sevenHearts = new ImageIcon("card/card/20.png");
        ImageIcon eightHearts = new ImageIcon("card/card/21.png");
        ImageIcon nineHearts = new ImageIcon("card/card/22.png");
        ImageIcon tenHearts = new ImageIcon("card/card/23.png");
        ImageIcon jackHearts = new ImageIcon("card/card/24.png");
        ImageIcon queenHearts = new ImageIcon("card/card/25.png");
        ImageIcon kingHearts = new ImageIcon("card/card/26.png");
        ImageIcon aceDiamonds = new ImageIcon("card/card/27.png");
        ImageIcon twoDiamonds = new ImageIcon("card/card/28.png");
        ImageIcon threeDiamonds = new ImageIcon("card/card/29.png");
        ImageIcon fourDiamonds = new ImageIcon("card/card/30.png");
        ImageIcon fiveDiamonds = new ImageIcon("card/card/31.png");
        ImageIcon sixDiamonds = new ImageIcon("card/card/32.png");
        ImageIcon sevenDiamonds = new ImageIcon("card/card/33.png");
        ImageIcon eightDiamonds = new ImageIcon("card/card/34.png");
        ImageIcon nineDiamonds = new ImageIcon("card/card/35.png");
        ImageIcon tenDiamonds = new ImageIcon("card/card/36.png");
        ImageIcon jackDiamonds = new ImageIcon("card/card/37.png");
        ImageIcon queenDiamonds = new ImageIcon("card/card/38.png");
        ImageIcon kingDiamonds = new ImageIcon("card/card/39.png");
        ImageIcon aceClubs = new ImageIcon("card/card/40.png");
        ImageIcon twoClubs = new ImageIcon("card/card/41.png");
        ImageIcon threeClubs = new ImageIcon("card/card/42.png");
        ImageIcon fourClubs = new ImageIcon("card/card/43.png");
        ImageIcon fiveClubs = new ImageIcon("card/card/44.png");
        ImageIcon sixClubs = new ImageIcon("card/card/45.png");
        ImageIcon sevenClubs = new ImageIcon("card/card/46.png");
        ImageIcon eightClubs = new ImageIcon("card/card/47.png");
        ImageIcon nineClubs = new ImageIcon("card/card/48.png");
        ImageIcon tenClubs = new ImageIcon("card/card/49.png");
        ImageIcon jackClubs = new ImageIcon("card/card/50.png");
        ImageIcon queenClubs = new ImageIcon("card/card/51.png");
        ImageIcon kingClubs = new ImageIcon("card/card/52.png");
        ImageIcon joker1 = new ImageIcon("card/card/53.png");
        ImageIcon joker2 = new ImageIcon("card/card/54.png");



        double x;
        x = Math.random();      
        x = (int)(54.0 * Math.random()) + 1;

        double y;
        y = Math.random();      
        y = (int)(54.0 * Math.random()) + 1;

        double z;
        z = Math.random();      
        z = (int)(54.0 * Math.random()) + 1;



        JLabel firstCard = new JLabel(x + ".png");

        JLabel secondCard = new JLabel(y + ".png");
        JLabel thirdCard = new JLabel(z + ".png");

        JFrame frame = new JFrame("Image Icon Example");
        frame.setLayout(new GridLayout(1, 2, 2, 2 ));
        frame.add(firstCard);
        frame.add(secondCard);
        frame.add(thirdCard);

        frame.setSize(500, 150);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);



    }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

You are using a different constructor

public JLabel(String text)

You should use another

public JLabel(Icon image)

All instances of ImageIcon keep in the array

ImageIcon[] iconArray;

since all your image files are numbered 1-54 this may save you some space.

import javax.swing.*;
import java.awt.*;

public class RandomCard extends JFrame {
    int firstRandomCard = 1 + (int)(Math.random() * 54);
    int secondRandomCard = 1 + (int)(Math.random() * 54);
    int thirdRandomCard = 1 + (int)(Math.random() * 54);

    private ImageIcon firstCard = new ImageIcon("card/card" + firstRandomCard + ".png");
    private ImageIcon secondCard = new ImageIcon("card/card" + secondRandomCard + ".png");
    private ImageIcon thirdCard = new ImageIcon("card/card" + thirdRandomCard + ".png");

  public RandomCard() {
    setLayout(new GridLayout(1, 3, 5, 5));
    add(new JLabel(firstCard));
    add(new JLabel(secondCard));
    add(new JLabel(thirdCard));
  }

  public static void main(String[] args) {
    RandomCard frame = new RandomCard();
    frame.setTitle("Three Random Cards");
    frame.setSize(250, 150);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

Edited 3 Years Ago by Dani: Formatting fixed

This question has already been answered. Start a new discussion instead.