java - initializing more than one JFrame creates blank JFrames -
i'm new swing , i'm having trouble replacing existing jframe. initialize first jframe without problem...
main:
public static jframe jf; public static int state = 0; public static void main(string args[]) { jf = new mainmenu(); jf.setvisible(true); while (state != -1) {} }
mainmenu:
public mainmenu() { settitle("battleship"); setdefaultcloseoperation(jframe.exit_on_close); setbounds(100, 100, 450, 300); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); setcontentpane(contentpane); jbutton btnsingleplayer = new jbutton("single player - easy"); btnsingleplayer.addmouselistener(new mouseadapter() { @override public void mouseclicked(mouseevent arg0) { gamedriver gd = new gamedriver(1); } }); jbutton btnsplitscreenmultiplayer = new jbutton("split screen multiplayer"); btnsplitscreenmultiplayer.addmouselistener(new mouseadapter() { @override public void mouseclicked(mouseevent arg0) { gamedriver gd = new gamedriver(0); } }); jbutton btnonlinemultiplayer = new jbutton("online multiplayer"); btnonlinemultiplayer.setenabled(false); jbutton btnhowtoplay = new jbutton("how play"); btnhowtoplay.addmouselistener(new mouseadapter() { @override public void mouseclicked(mouseevent e) { if (desktop.isdesktopsupported()) { try { //set page url in string. eg, m using url google search engine string url = "http://www.hasbro.com/common/instruct/battleship.pdf"; java.awt.desktop.getdesktop().browse(java.net.uri.create(url)); } catch (java.io.ioexception e2) { system.out.println(e2.getmessage()); } } } }); grouplayout gl_contentpane = new grouplayout(contentpane); gl_contentpane.sethorizontalgroup( gl_contentpane.createparallelgroup(alignment.leading) .addgroup(alignment.trailing, gl_contentpane.createsequentialgroup() .addcontainergap(128, short.max_value) .addgroup(gl_contentpane.createparallelgroup(alignment.trailing, false) .addcomponent(btnhowtoplay, alignment.leading, grouplayout.default_size, grouplayout.default_size, short.max_value) .addcomponent(btnonlinemultiplayer, alignment.leading, grouplayout.default_size, grouplayout.default_size, short.max_value) .addcomponent(btnsingleplayer, alignment.leading, grouplayout.default_size, grouplayout.default_size, short.max_value) .addcomponent(btnsplitscreenmultiplayer, alignment.leading)) .addgap(121)) ); gl_contentpane.setverticalgroup( gl_contentpane.createparallelgroup(alignment.leading) .addgroup(alignment.trailing, gl_contentpane.createsequentialgroup() .addcontainergap(89, short.max_value) .addcomponent(btnsingleplayer) .addpreferredgap(componentplacement.related) .addcomponent(btnsplitscreenmultiplayer) .addpreferredgap(componentplacement.related) .addcomponent(btnonlinemultiplayer) .addpreferredgap(componentplacement.related) .addcomponent(btnhowtoplay) .addgap(45)) ); contentpane.setlayout(gl_contentpane); }
but once enter gamedriver , try initialize different jframe...
gamedriver:
//stuff happens programshell.jf.setvisible(false); programshell.jf = new placementwindow(currentplayer, otherplayer, curship); programshell.jf.setvisible(true);
i blank jframe without of it's components (it created when use placementwindow's main initialize)
placementwindow:
private jpanel contentpane; private gameboard gbcur; private gameboard gboth; private int curplacement; private string title; private int sqsize = 21; private int xoffset = 27; private int yoffset = 27; private int divoffset = 283; public placementwindow(gameboard cur, gameboard oth, int curship) { this.gbcur = cur; this.gboth = oth; this.curplacement = curship; title = "battleship! " + cur.getname() + ", place ships"; settitle(title); setbounds(100, 100, 518, 389); setdefaultcloseoperation(jframe.exit_on_close); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); setcontentpane(contentpane); //stuff added content pane imagepanel leftboard = new imagepanel("graphicsassets/boardgraphic.png"); leftboard.setbounds(6, 6, 250, 250); getcontentpane().add(leftboard); //more stuff added content pane contentpane.setlayout(null); }
i recognize approach little unconventional, i'm trying work within limitations of code not designed swing (and must remain, part, intact). appreciated!
edit
i've modified code mimic vince's along following function:
public static void update(/*new values*/) { frame.remove(pp); secondpanel = new secondpanel(/*new values*/); frame.add(pp, "secondpanel"); switchpanel("secondpanel");
unfortunately frame doesn't switch original mainmenu frame upon calling update. ideas?
don't initialize new frames. if want switch 1 panel another, set frame's layout cardlayout, , add jpanels.
class myframe { static cardlayout cl = new cardlayout(); //handles panel switching static jframe frame; //init swing on edt static firstpanel firstpanel; static secondpanel secondpanel; public static void main(string[] args) { initframe(); } public static void initframe() { swingutilities.invokelater(new runnable() { public void run() { frame = new jframe(); frame.setdefaultcloseoperation(3); frame.setlayout(cl); firstpanel = new firstpanel(); secondpanel = new secondpanel(); //first panel added frame show first frame.add(firstpanel, "first"); frame.add(secondpanel, "second"); frame.pack(); //sizes frame fit panel being shown setvisible(true); } }); } public static void switchpanel(string name) { cl.show(frame.getcontentpane(), name); frame.pack(); } } class firstpanel extends jpanel { } class secondpanel extends jpanel { }
you use panels if needed, can switch/dispose/create panels instead of entire frame.
but, games, don't use panel approach. games use 1 panel, , instead. if doesn't make sense you, answered question simple game design: jframe, jpanel, keylistener questions
i suggest looking game states. big deal when comes games. allows render/update depending on state game @ (menu, game, pause, ect..)
Comments
Post a Comment