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

  • Home
  • SEARCH
  • 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 6643263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:01:50+00:00 2026-05-26T00:01:50+00:00

Im learning Java and Im creating a memory type game where you have to

  • 0

Im learning Java and Im creating a memory type game where you have to find two equal cards.

I have created a Window etc etc but my problem is adding multiple JButtons to it. (my cards are JButtons with icons). I have commented my code where my problem is.

//Get the images.
private File bildmapp = new File("bildmapp");
private File[] bilder = bildmapp.listFiles();
//My own class extending JButton
Kort[] k = new Kort[bilder.length];

for(int i = 0; i < bilder.length; i++){
        k[i] = new Kort(new ImageIcon(bilder[i].getPath()));
    }



//Later in my code:
    int sum = rows * columns;
    Kort[] temp = new Kort[sum];

            //My function to randomize.
    Verktyg.slumpOrdning(k);

            //***********************//
            //Trying to fill a array from K (which contains all cards) so my temp contains SUM cards and SUM/2 pairs
    for(int i = 0; i < sum/2; i++){
        temp[i] = k[i];
        temp[i+sum/2] = k[i];
    }


            //Problem is that i only get SUM/2 (half of the cards) cards, not the 16 (8 pairs) i would like to  add in this case
            //SYNLIGT = VISIBLE.
    for(int i = 0; i < sum; i++){
        temp[i].setStatus(Kort.Status.SYNLIGT);
        j.add(temp[i]);
    }
  • 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-05-26T00:01:50+00:00Added an answer on May 26, 2026 at 12:01 am

    Your code ends up adding each Kort object to the container twice, since the array temp contains two references to each Kort. When you add a Kort a second time, it moves to the second location. A Component can only appear in one place at a time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just starting out learning Java, GAE etc. I have started a project
just learning Java and I know this may sound stupid but I have to
So I'm learning java, and I have a question. It seems that the types
I'm learning Java by reading the online Java book, but im finding it hard
i am learning Java at the moment and have the following question: i am
I'm learning Java and OOP, and have been doing the problems at Project Euler
I'm learning Java right now but I'm wondering if there is a hierarchy of
I have to convert a WSDL to java classes for creating WS server and
I'm re-learning c++ (I have 10 years Java experience) and I'm also learning Qt
Im learning Java and having a problem with ArrayList. Firstly I have a class

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.