java - How to set an ImageIcon using a Switch statement -


alright, pretty brand new @ coding, trying.

i need create imageicon use switch statement determine image show.

i looked @ thread here: change image if statement

which recommended switch statement on if statement, thought help. when edit code, , change had before, error says "imageicon cannot resolved variable". i've tried variety of capitalization combos none of them working.i left code had underneath first case. code not giving me error messages i'm thinking should not creating new imageicon each case, since need pull 1 in program , have switch statement determine image display.

revised code

  public imageicon dieimage(string string)    {        imageicon dieimage = new imageicon("");        switch (facevalue){         case 1: dieimage = new imageicon ("src/1.jpg");        break;        case 2: dieimage = new imageicon("src/2.jpg");        break;        case 3: dieimage = new imageicon("src/3.jpg");        break;        case 4: dieimage = new imageicon("src/4.jpg");        break;        case 5: dieimage = new imageicon("src/5.jpg");        break;        case 6: dieimage = new imageicon("src/6.jpg");        break;        }     return dieimage;    } } 

any appreciated.

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

get rid of {} , use:

imageicon icon; 

now switch statement can assign proper icon used label.

//case 1: imageicon = ("src/1.jpg"); case 1: icon = new imageicon("src/1.jpg"); 

you have invalid syntax, fix code shown above.

your other statement nothing since create icon icon not assigned variable can used. code should be:

//case 2: new imageicon("src/2.jpg"); case 2: icon = new imageicon("src/2.jpg"); 

then use icon need update label containing icon:

label.seticon( icon ); 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -