Java Swing Interface Troubles -
this strangest bug i've ever encountered. whenever start program, in 1 of 2 times, gui created , pop-ups. in other 1 out of 2 times, no gui, no error , code executed in same order whenever gui created.
what causing bug? , how resolve this? i'm clueless..
the view class :
package view; import controller.gamecontroller; import lombok.data; import model.gamemodel; import javax.swing.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; @data public class view extends jframe { gameinterface gameinterface = new gameinterface(); mainmenuscreen menuscreen = new mainmenuscreen(); public view() { super("view"); setdefaultcloseoperation(windowconstants.exit_on_close); displaymenuscreen(); setvisible(true); } public void displaymenuscreen(){ menuscreen.getstartthegamebutton().addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { gamemodel.getinstance().newgame(); displaygameinterface(); gamecontroller.startgame(); } }); setcontentpane(menuscreen.getcontentpane()); } public void displaygameinterface(){ setcontentpane(gameinterface.getcontentpane()); } }
the main menu class:
package view; import lombok.data; import javax.swing.*; @data public class mainmenuscreen extends jframe{ private jbutton startthegamebutton; private jpanel rootpanelmainmenu; private jtextpane crazybananarepublictycoontextpane; public mainmenuscreen() { super("my game's name"); setdefaultcloseoperation(windowconstants.exit_on_close); setcontentpane(rootpanelmainmenu); system.out.println("heyhey3"); } }
the class view created:
import model.gamemodel; import model.gamemodelobserver; import view.view; public class crazybananarepublictycoon { public static final void main(string args[]){ view view = new view(); view.setvisible(true); gamemodelobserver gamemodelobserver = new gamemodelobserver(view.getgameinterface()); gamemodel.getinstance().addobserver(gamemodelobserver); gamemodel.getinstance().setgamemodelobserver(gamemodelobserver); } }
you not using event dispatch thread...
you must interact swing/awt objects via edt.
change main method:
public static final void main(string args[]) { eventqueue.invokelater(new runnable() { public void run() { view view = new view(); view.setvisible(true); gamemodelobserver gamemodelobserver = new gamemodelobserver(view.getgameinterface()); gamemodel.getinstance().addobserver(gamemodelobserver); gamemodel.getinstance().setgamemodelobserver(gamemodelobserver); } } }
Comments
Post a Comment