Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:18:56+00:00 2026-06-12T00:18:56+00:00

So I have this program I’m working on that displays 3 random cards in

  • 0

So I have this program I’m working on that displays 3 random cards in the frame with labels. So far I have it to assign a deck of cards to an array of ImageIcons and then shuffle them. My question is, How do I go about assigning them to JLabels now. Forgive me for the stupid question, as I know it is something simple but I’m very new to using GUI and it frightens me lol

here’s my code so far:

package assignment2;
import javax.swing.*;

import java.awt.*;
public class Assignment2 extends JFrame{
    public Assignment2(){

        setLayout(new GridLayout(3,1,5,5));
        add(new JLabel()); ///display one random card
        add(new JLabel()); ////display another random card
        add(new JLabel()); ////display another random card
    }
    public static void main(String[] args) {

        Assignment2 frame = new Assignment2();
        frame.setTitle("MAIN TO THE FRAME");
        frame.setSize(600,300);
        frame.setResizable(true);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final int FINAL_CARDS_NUMBER = 54;

        ImageIcon deckOfCards [] = new ImageIcon [FINAL_CARDS_NUMBER];

        for(int i = 0; i <FINAL_CARDS_NUMBER; i++){
            deckOfCards[i] = new ImageIcon("C:/card/" + (i + 1) + ".png");
        }

        for (int i = 0; i < FINAL_CARDS_NUMBER; i++) {
            int r = i + (int) (Math.random() * (FINAL_CARDS_NUMBER-i));
            ImageIcon t = deckOfCards[r];
            deckOfCards[r] = deckOfCards[i];
            deckOfCards[i] = t;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:18:58+00:00Added an answer on June 12, 2026 at 12:18 am

    First, remove the 3 lines where you add the labels:

    add(new JLabel());
    

    Then, in the place where you are getting the ImageIcon, do this:

    ImageIcon t = deckOfCards[r];
    JLabel label = new JLabel(t);
    add(label);
    

    However, I suggest you don’t set the layout and add components directly to your JFrame. You would better create a new JPanel and add that to the content pane of your JFrame:

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(3, 1, 5, 5));
    getContentPane().add(panel);
    

    Then, when you add the labels, add them to the panel.

    panel.add(new JLabel(t));
    

    Now that you updated the question, I am updating the answer below.
    First, declare 3 JLabel, instantiate them and add them to the panel as explained above.

    JLabel card1 = new JLabel();
    JLabel card2 = new JLabel();
    JLabel card3 = new JLabel();
    

    Then, after you read the ImageIcon for each, you can set the JLabel image this way:

    card1.setIcon(t);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this program that I am working on for class, I think the
I have this program that isnt working. char arr[200] ; char *p = arr;
I have this program that should execute a piece of code base on the
I have this simple program that computes salaries for four different worker types. It's
I have this program that returns a factorial of N. For example, when entering
I have this program that takes one argument for the source file and then
I have this program that check a website, and I want to know how
I have this program that at one point accesses os.getcwd(), but some times, depending
I have this program, which as you can see is pulling random pictures out
I have this program that is reading the test string from a textbox and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.