i'm working on menu, want make object orientated possible i've made separate class menu jpanel object. problem doesn't want add main jpanel. doing wrong , how should fix it?
main class:
package stackoverflow; import java.awt.cardlayout; import javax.swing.*; public class main { private jframe frame = new jframe(); private jpanel mainpanel = new jpanel(); private cardlayout cl = new cardlayout(); private gamepanel gp = new gamepanel(); public main(){ frame.setlocation(100, 100); frame.setsize(1200, 700); frame.settitle("rain | pre-alpha"); frame.setdefaultcloseoperation(jframe.exit_on_close); mainpanel.setlayout(cl); mainpanel.add(gp, "1"); frame.add(mainpanel); cl.show(mainpanel, "1"); frame.setvisible(true); } public static void main(string[] args) { new main(); } }
gamepanel class:
package stackoverflow; import java.awt.color; import javax.swing.jpanel; public class gamepanel { private jpanel gamepanel = new jpanel(); public gamepanel(){ gamepanel.setbackground(color.green); } }
you can't add class jframe
, jframe accept component
:
public component add(component comp, int index)
so have many ways solve problem :
option 1
extends jpanel
instead :
public class gamepanel extends jpanel { public gamepanel() { super.setbackground(color.green); } }
option 2
you can use getters , setters :
public class gamepanel { private jpanel gamepanel = new jpanel(); public jpanel getgamepanel() { return gamepanel; } public void setgamepanel(jpanel gamepanel) { this.gamepanel = gamepanel; } public gamepanel() { gamepanel.setbackground(color.green); } }
and can add jpanel :
mainpanel.add(gp.getgamepanel(), "1");
Comments
Post a Comment