samedi 28 mars 2015

How to set an ImageIcon using a Switch statement

Alright, I am pretty much brand new at coding, but I am trying.


I need to create an ImageIcon that will use a switch statement to determine which image to show.


I looked at the thread here: Change image with if statement


which recommended a switch statement over an if statement, so I thought it would help. But when I edit my code, and change it from what I had before, I get an error that says "ImageIcon cannot be resolved to a variable". I've tried a variety of capitalization combos but none of them are working.I left the code I originally had underneath the first case. That code is not giving me any error messages but I'm thinking I should not be creating a new ImageIcon for each case, since I need to pull from just one in the program and have the switch statement determine which image to display.


This is the entirety of the code that I have for creating the ImageIcon and the switch statement.



{
ImageIcon icon = new ImageIcon("");
}

{
switch (faceValue){



case 1: ImageIcon = ("src/1.jpg");
break;
case 2: new ImageIcon("src/2.jpg");
break;
case 3: new ImageIcon("src/3.jpg");
break;
case 4: new ImageIcon("src/4.jpg");
break;
case 5: new ImageIcon("src/5.jpg");
break;
case 6: new ImageIcon("src/6.jpg");
break;
}


I also need to be able to insert this created ImageIcon into a label in another class. I tried to create a method like setDieFace1 = icon but I'm clearly way off.


Any help would be appreciated.


Aucun commentaire:

Enregistrer un commentaire