java - JFrame won't load new JFrame when button pressed -


so have project i'm working on user choose between 5 games. whenever click on button play game wont load game. please in advance!

main jframe class:

import javax.swing.uimanager.lookandfeelinfo; import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener;   import java.awt.event.keyadapter; import java.awt.event.keyevent; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.event.mousewheelevent; import java.awt.event.mousewheellistener; import javax.swing.border.border; import java.io.randomaccessfile; import javax.swing.*; import java.awt.borderlayout; import java.io.ioexception; import java.util.*; import java.io.*; public class javagames extends jframe  {     private jmenubar menubar;     public static int height;     public static int width;     private jtextfield test;     private jbutton brickbreaker;     private jbutton exit;     private jbutton trapped;     private jbutton pacman;     private jbutton pinball;     private jbutton pong;     private jtextfield textfield1;     private jpanel contentpane;     public javagames() throws ioexception     {         this.settitle("application project");         this.setsize(449,350);         dimension screensize = toolkit.getdefaulttoolkit().getscreensize();         height = screensize.height;         width = screensize.width;         generatemenu();         this.setjmenubar(menubar);         contentpane = new jpanel(null);         contentpane.setpreferredsize(new dimension(500,400));         contentpane.setbackground(new color(192,192,192));         brickbreaker = new jbutton();         brickbreaker.setbounds(108,69,95,35);         brickbreaker.setbackground(new color(214,217,223));         brickbreaker.setforeground(new color(0,0,0));         brickbreaker.setenabled(true);         brickbreaker.setfont(new font("sansserif",0,11));         brickbreaker.settext("brick breaker");         brickbreaker.setvisible(true);         brickbreaker.addactionlistener(new actionlistener() {             public void actionperformed(actionevent evt) {                brickbreaker(evt);             }         });         exit = new jbutton();         exit.setbounds(219,175,95,35);         exit.setbackground(new color(214,217,223));         exit.setforeground(new color(0,0,0));         exit.setenabled(true);         exit.setfont(new font("sansserif",0,12));         exit.settext("exit");         exit.setvisible(true);         exit.addactionlistener(new actionlistener() {             public void actionperformed(actionevent evt) {                 exit(evt);             }         });         trapped = new jbutton();         trapped.setbounds(219,69,95,35);         trapped.setbackground(new color(214,217,223));         trapped.setforeground(new color(0,0,0));         trapped.setenabled(true);         trapped.setfont(new font("sansserif",0,12));         trapped.settext("trapped");         trapped.setvisible(true);         trapped.addactionlistener(new actionlistener() {             public void actionperformed(actionevent evt) {                 trapped(evt);             }         });         pacman = new jbutton();         pacman.setbounds(219,120,95,35);         pacman.setbackground(new color(214,217,223));         pacman.setforeground(new color(0,0,0));         pacman.setenabled(true);         pacman.setfont(new font("sansserif",0,11));         pacman.settext("pac man demo");         pacman.setvisible(true);         pacman.addactionlistener(new actionlistener() {             public void actionperformed(actionevent evt) {                 pacman(evt);             }         });         pinball = new jbutton();         pinball.setbounds(108,175,95,35);         pinball.setbackground(new color(214,217,223));         pinball.setforeground(new color(0,0,0));         pinball.setenabled(true);         pinball.setfont(new font("sansserif",0,12));         pinball.settext("pin ball");         pinball.setvisible(true);         pinball.addactionlistener(new actionlistener() {             public void actionperformed(actionevent evt)              {                 try{                 pinball(evt);                 }catch( ioexception io ) {}             }         });         pong = new jbutton();         pong.setbounds(108,120,95,35);         pong.setbackground(new color(214,217,223));         pong.setforeground(new color(0,0,0));         pong.setenabled(true);         pong.setfont(new font("sansserif",0,12));         pong.settext("pong");         pong.setvisible(true);         pong.addactionlistener(new actionlistener() {             public void actionperformed(actionevent evt) {                 pong(evt);             }         });         textfield1 = new jtextfield();         textfield1.setbounds(160,12,100,49);         textfield1.setbackground(new color(255,255,255));         textfield1.setforeground(new color(0,0,0));         textfield1.setenabled(false);         textfield1.setfont(new font("sansserif",0,18));         textfield1.settext("welcome!");         textfield1.setvisible(true);         contentpane.add(brickbreaker);         contentpane.add(exit);         contentpane.add(trapped);         contentpane.add(pacman);         contentpane.add(pinball);         contentpane.add(pong);         contentpane.add(textfield1);         this.add(contentpane);         this.setdefaultcloseoperation(jframe.exit_on_close);         this.setlocationrelativeto(null);         this.pack();         this.setvisible(true);       }     public static string[] q = new string[1];     private void brickbreaker(actionevent evt)      {         brickgui temp = new brickgui();         temp.main(q);     }     private void exit (actionevent evt)      {         int n=0;         system.exit(n);     }     private void trapped(actionevent evt)      {         trappedgui run = new trappedgui();         run.main(q);     }     private void pacman (actionevent evt)      {         pacmangui run = new pacmangui();         run.main(q);     }     private void pinball (actionevent evt) throws ioexception     {         pinballgui run = new pinballgui();         run.main(q);     }     private void pong (actionevent evt)      {         ponggui run = new ponggui();         run.main(q);     }     public void generatemenu()     {         menubar = new jmenubar();         jmenu file = new jmenu("file");         jmenu tools = new jmenu("tools");         jmenuitem openhs = new jmenuitem("high scores");         jmenuitem save = new jmenuitem("save");         jmenuitem exit = new jmenuitem("exit");         jmenuitem = new jmenuitem("about");         file.add(openhs);         file.add(save);         file.add(about);         file.addseparator();         file.add(exit);         menubar.add(file);         openhs.addactionlistener(new actionlistener(){             public void actionperformed(actionevent arg0){                 //highscores run = new highscores();                //run.main(q);             }         });         exit.addactionlistener(new actionlistener(){             public void actionperformed(actionevent arg0){                int n = 0;                system.exit(n);             }         });                 about.addactionlistener(new actionlistener(){             public void actionperformed(actionevent arg0){                 //about action             }         });        }      public static void main(string[] args){         system.setproperty("swing.defaultlaf",      "com.sun.java.swing.plaf.nimbus.nimbuslookandfeel");         javax.swing.swingutilities.invokelater(new runnable() {             public void run() {                 try{                 new javagames();                 }catch( ioexception io ) {}             }         });     } } 

jframe class load game:

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class brickgui extends jframe {     public brickgui()      {         brickgame gui = new brickgame();         this.setsize(600,600+20);         this.settitle("brick breaker");         this.setdefaultcloseoperation(jframe.exit_on_close);         this.setlocationrelativeto(null);         this.setvisible(true);         this.getcontentpane().add(new brickgame());         this.add(gui);         gui.init(600,600);         gui.start();         gui.run();     }     public static void main(string[] args)     {         new brickgui();       } } 

class game drawn , such:

import java.awt.*; import javax.swing.*; import java.applet.*; import java.util.*; import java.io.*; import java.awt.event.*; import java.awt.event.keylistener; public class brickgame extends jpanel implements runnable,keylistener {         thread runner;      private image buffer;     private graphics gbuffer;     private char c = 'e';     int width,height;     brickball b1;     arraylist brickballlist;     brickball temp,temp2;     arraylist bricks;     brickrectangle rtemp;     int bx,by,bw,bh;     brickrectangle r1,r2;     boolean leftkey;     boolean rightkey;     boolean spacekey;     paddle p;     int points;     public void init(int w,int h)    {           width=590;         height=590;         buffer=createimage(width,height);         gbuffer=buffer.getgraphics();         b1 = new brickball(-1);         p = new paddle();         bricks = new arraylist();         bx = 5;         = 5;         bw = 20;         bh = 10;         int count = 0;        for(int = 0; < 160;i++)        {            rtemp = new brickrectangle(bx,by,bw,bh,0,0);            bx = bx + 25;            count++;            if(count == 32)            {                = + 15;                bx = 5;                count = 0;            }         bricks.add(rtemp);     }     addkeylistener(this);     points = 0; } public void start() {      if (runner == null)     {            runner = new thread (this);         runner.start();     } } public void stop() {       runner = null; } public void run()  {       while(true)     {         if(rightkey){p.moveright();}         if(leftkey){p.moveleft();}         repaint();         try {runner.sleep(25);}         catch (exception e) { }             gbuffer.setcolor(color.black);         gbuffer.fillrect(0,0,width,height);         b1.movebrickball(width,height);         p.ballcollision(b1);         for(int j = 0; j < bricks.size();j++)         {              rtemp = (brickrectangle)bricks.get(j);              if(rtemp.brickballcollision(b1))              {                  bricks.remove(j);                  points = points + 1;                  break;              }         }         string p = string.valueof(points);         gbuffer.setcolor(color.blue);         gbuffer.setfont(new font("calibri",20,60));         gbuffer.drawstring(p,0,540);         for(int = 0; < bricks.size();i++)         {             rtemp = (brickrectangle)bricks.get(i);             rtemp.paint(gbuffer);         }         b1.paint(gbuffer);         p.paint(gbuffer);         if(b1.gety()>height)         {             string s1 = "game over";             string s2 = "you lose!";             string s3 = "you got " +points +" points";             string s4 = "hit space key continue";             gbuffer.drawstring(s1,50,90);             gbuffer.drawstring(s2,50,160);             gbuffer.drawstring(s3,50,230);             gbuffer.setfont(new font("calibri",20,50));             gbuffer.drawstring(s4,50,300);             brickhs temp = new brickhs();             try{                 boolean t = temp.checkhs(points);                 if(t)                 {                     gbuffer.drawstring("new high score",50,360);                 }             }catch( ioexception io ) {}             if(spacekey){system.exit(1);}         }         repaint();     }//while(true) } //run     public void update(graphics g) {       paint(g); }  public void paint(graphics g) {     g.drawimage(buffer,0,0, this); } public void addnotify()  {     super.addnotify();     requestfocus(); } public void keypressed(keyevent e)  {     switch(e.getkeycode())        {            case keyevent.vk_left:            leftkey = true;            break;            case keyevent.vk_right:            rightkey = true;            break;            case keyevent.vk_space:            spacekey = true;            break;        } } public void keyreleased(keyevent e)  {     switch(e.getkeycode())        {            case keyevent.vk_left:            leftkey = false;            break;            case keyevent.vk_right:            rightkey = false;            break;            case keyevent.vk_space:            spacekey = false;            break;         } } public void keytyped(keyevent e)  {     c = e.getkeychar();     repaint(); } } 

there few other classes go game make objects , not. again in advance!

whenever brickgame created, enters infinite while loop calling run - occurring on event dispatch thread (edt) , leads frozen user interface edt cannot paint, create events, etc... code starts new thread calling start - there no reason additional call run.

some other pieces of advice:

  1. override paintcomponent rather paint
  2. call super.paintcomponent 1 of first calls within implementation in (1)
  3. you should not override update, nor addnotify
  4. setting reference of thread null not stop thread - instead, use flag cases while loop terminate (this flag may need declared volatile).
  5. variable names should start lower case letter.
  6. avoid null layouts - use appropriate layoutmanager

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -