java - Why i have to overMouse to get JButtons Visible on first running? -


im build 2d board game,and when run main class,the frame openning ok, jbuttons aren't visible,they turning visible when come on mouse on buttons. idea?

before mouseover image: http://postimg.org/image/iyzvmvz6t

after mouseover image: http://postimg.org/image/80om4palh

edit: discoverd if minimize window,all jbuttons disappear again, hope guys..

paneltest.java:

  import java.awt.*; import java.awt.event.*; import java.io.*;  import javax.imageio.*; import javax.swing.*;  import sun.audio.*;  public class paneltest {         public static void main(string[] args)     {           imageframe frame = new imageframe();         frame.setresizable(false);      } }  class imageframe extends jframe {      public imageframe()     {            super();         settitle("jumping beans");         setsize(default_width, default_height);         container c=getcontentpane();         c.setlayout(new borderlayout());         imagepanel panel = new imagepanel();         panel.setpreferredsize(new dimension(650, 650));         c.add(panel,borderlayout.center);         panel.whostart();          jpanel sidebar = new jpanel();         sidebar.setpreferredsize(new dimension(200, 650));         panel.setalignmentx(component.center_alignment);         jbutton exit=new jbutton("exit");         exit.addactionlistener(new actionlistener()          {                @override             public void actionperformed(actionevent e)              {                 system.exit(dispose_on_close);             }         });         jbutton musicstart=new jbutton("play music");         musicstart.addactionlistener(new actionlistener()          {                 @override             public void actionperformed(actionevent e)              {                 panel.music();              }         });          sidebar.setbackground(color.lightgray);         sidebar.add(exit);         sidebar.add(musicstart);         c.add(sidebar, borderlayout.east);          jpanel bottom = new jpanel();         bottom.setbackground(color.lightgray);         panel.turn = new jlabel();         bottom.add(panel.turn);         c.add(bottom, borderlayout.south);            this.setdefaultcloseoperation(jframe.exit_on_close);         panel.changelabel();         this.setvisible(true);        }      public static final int default_width = 850;     public static final int default_height = 650;  }  class imagepanel extends jpanel {       public imagepanel()     {           super();         this.setlayout(new gridlayout(11,11));         al=new actionlistener()         {             int numclicks =0 ;             slot source;             slot target;             slotbutton sourcebutton;             slotbutton targetbutton;              @override             public void actionperformed(actionevent e)              {                  if(numclicks==0)                 {                     if(boardi.whowins(boardi.gets()))                     {                         sourcebutton=(slotbutton) e.getsource();                         source=new slot(sourcebutton.getx(),sourcebutton.gety(),sourcebutton.getcolor());                         if(boardi.gettor()!=source.getcolor())                             numclicks--;                         system.out.println("source num "+source.getrow()+","+source.getcol()+" clicked");                         numclicks++;                     }                 }                 else if(numclicks==1)                 {                      targetbutton=(slotbutton) e.getsource();                     target=new slot(targetbutton.getx(),targetbutton.gety(),targetbutton.getcolor());                     system.out.println("target num "+target.getrow()+","+target.getcol()+" clicked");                     if(boardi.logiismoving(source,target))                     {                         updatelogi(source,target);                         updategraphfromlogi(buttons, boardi);                         changetor();                         changelabel();                         //boardi.printboard();                       }                     else if(boardi.logiiseating(source,target))                     {                         system.out.println("doing iseating");                         updatelogieat(source, target);                         updategraphfromlogi(buttons, boardi);                         changetor();                         changelabel();                         if(!boardi.whowins(boardi.gets()))                             endgame();                         //boardi.printboard();                      }                     else                         if(target==source)numclicks++;                     numclicks--;                 }               }           };           try         {             image = imageio.read(new file("images/background.png"));             bluebean = imageio.read(new file("images/blue_bean.png"));             redbean = imageio.read(new file("images/red_bean.png"));             empty = imageio.read(new file("images/empty.png"));             safewall = imageio.read(new file("images/safewall.bmp"));         }         catch (ioexception e)         {             e.printstacktrace();         }          buttons=new slotbutton[11][11];          for(int i=0;i<11;i++)         {             for(int j=0;j<11;j++)             {                 buttons[i][j]=new slotbutton(i,j,boardi.gets()[i][j].getcolor());                 buttons[i][j].addactionlistener(al);                 this.add(buttons[i][j]);              }         }         //repaint();      }  public slotbutton [][] getbuttons()  {     return buttons; } public void endgame() {     char winner=boardi.whowinner(boardi.gets());     for(int i=0;i<11;i++)         for(int j=0;j<11;j++)             buttons[i][j].setenabled(false);     winnerlabel(winner); }  public void updatelogi(slot source,slot target) {     boardi.gets()[target.getrow()][target.getcol()].setcolor(source.getcolor());     boardi.gets()[source.getrow()][source.getcol()].setcolor('e'); }  public void updatelogieat(slot source,slot target) {     boardi.gets()[target.getrow()][target.getcol()].setcolor(source.getcolor());     boardi.gets()[source.getrow()][source.getcol()].setcolor('e');     boardi.gets()[boardi.getxofmiddle(source, target)][boardi.getyofmiddle(source, target)].setcolor('e');     system.out.println("middle row= "+boardi.gets()[boardi.getxofmiddle(source, target)][boardi.getyofmiddle(source, target)].getrow()+", middle col= "+boardi.gets()[boardi.getxofmiddle(source, target)][boardi.getyofmiddle(source, target)].getcol()+",middle color= "+boardi.gets()[boardi.getxofmiddle(source, target)][boardi.getyofmiddle(source, target)].getcolor()); }  public void updategraphfromlogi(slotbutton[][] buttons,board boardi)  {     char color='e';     for(int i=0;i<11;i++)         for(int j=0;j<11;j++)         {             color=boardi.gets()[i][j].getcolor();             buttons[i][j].setcolor(color);         } }   //======================until here logi rules======================================    public void changelabel()  {     char t=boardi.gettor();     system.out.println("changelabel="+t);     switch(t)     {     case 'r': turn.settext("its turn of red player");     break;      case 'b': turn.settext("its turn of blue player");     break;     } } public void winnerlabel(char winner)  {     switch(winner)     {     case 'r': turn.settext("its winning of red player!");     break;      case 'b': turn.settext("its winning of blue player!");     break;      case 'd': turn.settext("its draw!");     break;      case 'o': turn.settext("you have finish game in 1 move!");     break;      case 'x':break;     } }  public void printbordi(board a) {     for(int i=0;i<7;i++)     {         system.out.printf("-----------------------------------");         system.out.printf("\n");         for(int j=0;j<7;j++)             system.out.printf("| %c |",a.gets()[i][j].getcolor());         system.out.printf("\n");     }     system.out.printf("-----------------------------------"); }  public board getboard() {     return boardi; } public void changetor() {     char tor=boardi.gettor();     if(tor=='b')     {         boardi.settor('r');     }      else         boardi.settor('b');     system.out.println("changedtor="+boardi.gettor()); }  public void whostart() {     string input="";     input=joptionpane.showinputdialog("enter color of first player, b blue , r red");     system.out.println("input="+input);     if((input.equals("b"))||(input.equals("b")))         boardi.settor('b');     else if((input.equals("r"))||(input.equals("r")))         boardi.settor('r');     system.out.println("tor="+boardi.gettor()); }  //=================================from here finish game rules===========================   public int getturn() {     return comorhum; } public void setturn(int num) {     this.comorhum+=num; } jlabel turn; private actionlistener al; private image image; public int comorhum=1; public static image redbean; public static image bluebean; public static image empty; public static image safewall; private int numclicks=0; private board boardi = new board(); private slotbutton[][] buttons;  //=================music=================== public static void music()  {        audiostream bgm=null;      audioplayer  mgp = audioplayer.player;      audiodata md;      continuousaudiodatastream loop = null;      try     {         inputstream test = new fileinputstream("music/gamemusic.wav");         bgm = new audiostream(test);         audioplayer.player.start(bgm);     }     catch(filenotfoundexception e){         system.out.print(e.tostring());     }     catch(ioexception error)     {         system.out.print(error.tostring());     }     mgp.start(loop);  } //============side bar buttons========================================  } 

board.java:

     import java.awt.image;      import java.util.arraylist;  public class board extends paneltest {      private slot[][] s = new slot[11][11];     private char tor='b';     public board()     {         super();         for(int i=0;i<2;i++)             for(int j=0;j<11;j++)                 s[i][j]=new slot(i,j,'x');         for(int i=9;i<11;i++)             for(int j=0;j<11;j++)                 s[i][j]=new slot(i,j,'x');         for(int i=0;i<2;i++)             for(int j=2;j<9;j++)                 s[j][i]=new slot(i,j,'x');         for(int i=9;i<11;i++)             for(int j=2;j<9;j++)                 s[j][i]=new slot(i,j,'x');         for(int i=2;i<5;i++)             for(int j=2;j<9;j++)                 s[i][j]=new slot(i,j,'b');         for(int i=6;i<9;i++)             for(int j=2;j<9;j++)                 s[i][j]=new slot(i,j,'r');         for(int j=2;j<5;j++)             s[5][j]=new slot(5,j,'b');         for(int j=6;j<9;j++)             s[5][j]=new slot(5,j,'r');         s[5][5]=new slot(5,5,'e');      }       public board newboard(slot[][] b)      {          board f=new board();         f.s=b;         return f;     }       public slot[][] gets()      {         return s;     }      public slot getslot(int i,int j)     {         return s[i][j];     }      public void printboard()     {         for(int i=0;i<11;i++)         {             system.out.printf("-----------------------------------");             system.out.printf("\n");             for(int j=0;j<11;j++)                 system.out.printf("| %c |",s[i][j].getcolor());             system.out.printf("\n");         }         system.out.printf("-----------------------------------");     }      //==================================================================      public boolean logicheckmove(slot source,slot target)     {         if((source.getrow()==target.getrow())&&(source.getcol()==target.getcol())) // source , target same slot             return false;                if((target.getcolor()!='e')||(source.getcolor()=='e'))             return false;            system.out.println("logicheckmove true");         return true;     }      public boolean logiemptynei(slot source,slot target)     {         int row=source.getrow();         int col=source.getcol();         if(row<=8)         {             if(s[row+1][col].getcolor()=='e')                 //if(s[row+1][col]==target)                 return true;         }         if(row>=2)         {             if(s[row-1][col].getcolor()=='e')                 //if(s[row-1][col]==target)                 return true;         }         if(col<=8)         {             if(s[row][col+1].getcolor()=='e')                 //if(s[row][col+1]==target)                 return true;         }         if(col>=2)         {             if(s[row][col-1].getcolor()=='e')                 //if(s[row][col-1]==target)                 return true;         }         return false;     }      public boolean logiismoving(slot source,slot target)     {         int row=source.getrow();         int col=source.getcol();         system.out.println("source x="+source.getrow()+"y="+source.getcol());         system.out.println("target x="+target.getrow()+"y="+target.getcol());         if(!logicheckmove(source,target))         {                system.out.println("the move illigel");             return false;         }         if(!logiemptynei(source,target))         {                system.out.println("the move illigel2");             return false;         }         if(!distanceisokformoving(source,target))             return false;         system.out.println("the move ligel");          return true;     }      public boolean distanceisokformoving(slot source,slot target)     {         if((distanceofrow(source,target)!=1)||(distanceofcol(source,target)!=1))             return false;         return true;     }     public int distanceofrow(slot source, slot target)      {          return math.abs(source.getrow()-target.getrow());     }     public int distanceofcol(slot source, slot target)      {          return math.abs(source.getcol()-target.getcol());     }       public boolean logiiseating(slot source,slot target)     {         system.out.println("inside iseating");         int row=source.getrow();         int col=source.getcol();         if(!logicheckmove(source,target))         {                system.out.println("the eating illigel");             return false;         }         if(!logirighteat(source,target))         {             system.out.println("not righteat");             return false;         }          return true;      }     public boolean logirighteat(slot source, slot target)      {             //not wrote yet         return true;     }      public boolean whowins(slot [][] board)     {         int bluecount=0,redcount=0;         for(int i=2;i<9;i++)             for(int j=2;j<9;j++)                 if(board[i][j].getcolor()=='r')                     redcount++;                 else  if(board[i][j].getcolor()=='b')                     bluecount++;         if(redcount==0)         {             system.out.println("blue wins!");             return false;         }         else if(bluecount==0)         {             system.out.println("red wins!");             return false;         }         else if((bluecount==1)&&(redcount==1))         {             slot source=null;             slot target=null;             for(int i=2;i<9;i++)                 for(int j=2;j<9;j++)                     if(board[i][j].getcolor()=='r')                         source=s[i][j];                     else  if(s[i][j].getcolor()=='b')                         target=s[i][j];             if(logiiseating(source,target))             {                 system.out.println("you have finish game in 1 move!");             }             else             {                 system.out.println("draw!");                 return false;             }          }         return true;     }      public char whowinner(slot [][] board)     {         int bluecount=0,redcount=0;         for(int i=2;i<9;i++)             for(int j=2;j<9;j++)                 if(board[i][j].getcolor()=='r')                     redcount++;                 else  if(board[i][j].getcolor()=='b')                     bluecount++;         if(redcount==0)         {             return 'b';         }         else if(bluecount==0)         {             return 'r';         }         else if((bluecount==1)&&(redcount==1))         {             slot source=null;             slot target=null;             for(int i=2;i<9;i++)                 for(int j=2;j<9;j++)                     if(board[i][j].getcolor()=='r')                         source=s[i][j];                     else  if(s[i][j].getcolor()=='b')                         target=s[i][j];             if(logiiseating(source,target))             {                 return 'o';             }             else             {                 return 'd';             }          }         return 'x';     }        public int getxofmiddle(slot source,slot target)     {         int row=source.getrow();         if(target.getrow()-2==row)             return row+1;         else if(target.getrow()+2==row)             return row-1;         return row;     }      public int getyofmiddle(slot source,slot target)     {         int col=source.getcol();         if(target.getcol()-2==col)             return col+1;         else if(target.getcol()+2==col)             return col-1;         return col;     }      public char gettor() {         return tor;     }       public void settor(char tor) {         this.tor = tor;     }        } 

not related problem but:

  1. variable names should not start upper case character. of names correct, many not. consistent!

  2. method names should not start upper case character. again, correct, many not. there no reason inconsistent!

  3. class names should start upper case character. again, don't.

  4. the setresizable(true) statement should invoked before make frame visible. (it may need invoked before setsize() method, i'm not sure). in case should not executed after frame visible.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -