swing - Switching between multiple JPanels within a JFrame -


http://imgur.com/vt3jhh8 trying create start menu based game. wondering if approach good. have created frame class "sonomaroller" shown below. in class adds "frame" "panel1" shown in diagram. of have images drawn in class appear "panel2". want user able switch between panels buttons in panel1. of panels panel 2 have buttons of own well. best approach this? should make seperate classes panels , add them in jframe? should switch between panels using jframe since adds first panel? have included code below 2 classes. have jtext pane below panels are. in advance

___________________my frame___________________

package sonomaroller;  import javax.swing.*; import java.awt.*; import static javax.swing.jframe.*;  public class sonomaroller extends jframe {      public static dimension size = new dimension(550,550); //dimension of frame     public static string title = "sonoma roller v0.00" ;     //creates new object f create window      //boolean     public boolean addshop=false;      public sonomaroller(){        settitle(title);         setsize(size);         setresizable(false);         setlocationrelativeto(null); // null centers window on screen         setdefaultcloseoperation(jframe.exit_on_close);          init(addshop);     }     public void init(boolean addshop){        frame panel=new frame();         panel.setlayout(null);        add(panel);        setvisible(true);      }    public static void main(string[] args) {         sonomaroller object1=new sonomaroller();        } } 

___________________my panel w/buttons___________________

package sonomaroller; import javax.swing.*; import java.awt.*;  import java.awt.event.*; import javax.swing.text.badlocationexception; import javax.swing.text.defaultcaret;  import javax.swing.text.styleddocument; import javax.swing.text.style; import javax.swing.text.styleconstants;  public class frame extends jpanel implements runnable {       public void run(){      }      public frame(){          loadpics();         attackbutton();         magicbutton();         travelbutton();         shopbutton();         textfield();       }     public void paintcomponent(graphics g){       }     }     public void textfield(){   }     public void attackbutton(){      }     public void magicbutton(){      }     public void travelbutton(){      }     public void shopbutton(){      }       public void loadpics(){         sonoma = new imageicon("c:\\users\\camtronius\\documents\\netbeansprojects\\sonomaroller\\src\\sonomaroller\\testimage.jpg").getimage();         system.out.println("image loaded");           loaded = true;         repaint();     }  } 

can not use actionlistener set panel visible or invisible pressing button. example:

panel_1button.addactionlistener(new actionlistener(){         public void actionperformed(actionevent e){             panel1.setvisible(false)             panel2.setvisible(true);         }     }); 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -