java - The switch statent and return values - How to use a switch to determin JPanel drawn -
i have program has different jpanels written in different classes. print particular jpanel depending on button clicked user.
when program launched, has 3 buttons: "animals jbutton", "plants jbutton" , "refresh jbutton" in jframe "frame"; no jpanel.
if, example, user clicks on "animals jbutton", jpanel animals gets printed on jframe.
the "animalsjpanel" , "plantsjpanel" written in different classes. class, "pagereturner", has method determines gets printed via switch.
public class redirect { string pageanimals = "pageanimals"; string pageplants = "pageplants"; string value; public string pageredirect (string pageid) { switch (pageid) { case pageanimals: value = (animalsjpanel animalsjpanel = new animalsjpanel()); break; case pageplants: value = (plantsjpanel plantsjpanel = new plantsjpanel()); break; case 2: value = null; break; } return null; } }
i "constant string expression required" , "incopatible types" errors in netbeans. might problem switch statement, , there better way go this, i.e, determining page print. i'd code , not use cardlayout. i'm new java , trying learn how objects classes.
is switch statement done. i'm teaching myself programming, , have no 1 consult. appreaciation in advance advice
declare variable final used in switch case
.
final string pageanimals = "pageanimals"; final string pageplants = "pageplants";
since case
labels should compile time constants in switch
.
Comments
Post a Comment