Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Constructor help

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Constructor help

    hey im needing a bit of help with a little project im working on so i hoping some one could push me in the right direction.

    Basically, I'm creating a matching pairs game. (4 pairs of matching cards, the user has to find the matching pairs, simple enough right?).

    Each card will be represented by a JButton and ImageIcon, then when the image is clicked i'll use an event handler to change from the back to the face of the card. Then i'll use another class to compare the cards, if they match the user scored a hit if not they score a miss, then this will all be put in a GUI.

    I created a 'Card' class and im assuming im going to use a constructor method to create each individual card (inc JButton). What i'm thinking is this..


    Code(int i, String name, String imgName, String location) {
        int value = i;   // value of card for comparison
        ImageIcon imgname = new ImageIcon(location);
        JButton name = new JButton(imgName);
    }

    where int i will be a value assigned to each pair so i can compare them with an if statement, String name will be the name of the button, String imgName will be the ImageIcon name and String location will the image filename

    Then once all the the cards are created i'll put them in a JPanel and use GridLayout to set them out like cards dealt on a table.....

    Now i'm pretty sure what im doing is wrong but i after much research and mining through pages in textbooks i still can't find out why. Any help would be much appretiated.

    Thanks


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Constructor help

    In the code statement, it's meant to say

    Cards(....) {
    .
    .
    }

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,108
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Constructor help

    Can you explain your problem? Do you get error messages? If so, please post them.

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Constructor help

    One problem I found, in this code:

    JButton name = new JButton(imgName);

    particularly the

    JButton name

    part. You are defining (or trying to define) the variable "name" as a JButton, but it is already define as a String in your parameter variable list:

    public Card(int i, String name, String imgName, String location)

    Also, (I'm not 100% sure, but) I think your constructor has to be declared as public.

    I don't know if I understand you correctly, but I think you're trying to name a variable in your code based on another String, which I'm pretty sure you cannot do. For example, if I get the user's input (say it's "cake"), I don't think there's a way to name a variable in my code "cake" solely based on the user's (or coder's) String-based input.

    Hopefully this helped...

Similar Threads

  1. constructor X is undefined
    By ober0330 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 24th, 2011, 01:51 PM
  2. [SOLVED] Error in constructor
    By hello_world in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 14th, 2011, 07:46 PM
  3. Copy Constructor
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2011, 10:43 PM
  4. [SOLVED] Cannot Find Constructor
    By jrookie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 8th, 2010, 01:54 PM
  5. Point constructor
    By upad in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 5th, 2010, 07:34 PM