Java Swing Button not appearing on the form -


i having trouble form having grid of buttons appear. form supposed display grid of buttons used platform simple battleship game.

this code panel player grid

package view;  import java.util.arraylist; import java.util.list;  import javax.swing.jpanel;  public class playerpanel extends jpanel {      private list<positionbutton> buttonlist;       public playerpanel()     {          buttonlist = new arraylist<positionbutton>();         for(int = 0;i < 10;i++)             for(int j = 0;j < 10;j++)             {                 buttonlist.add(new positionbutton(i,j));             }         while(buttonlist.iterator().hasnext())         {             this.add(buttonlist.iterator().next());         }         while(buttonlist.iterator().hasnext())         {             buttonlist.iterator().next().setvisible(true);         }     }  } 

this class actual form:

package view;  import java.awt.dimension; import java.awt.gridlayout;  import javax.swing.jframe; import javax.swing.jpanel;  public class gameform {       private jframe theframe;     private jpanel boardpanel;     private playerpanel p1panel;     private playerpanel p2panel;        public gameform()     {         //set frame         theframe = new jframe();         theframe.setsize(new dimension(800,500));         theframe.setvisible(true);         theframe.setresizable(false);         theframe.setdefaultcloseoperation(jframe.exit_on_close);         //set board panel         boardpanel = new jpanel();         theframe.add(this.boardpanel);         //set first players board         p1panel = new playerpanel();         p1panel.setlayout(new gridlayout(0,0));         this.boardpanel.add(p1panel);         p1panel.setsize(new dimension(400,250));         //set second players boards         p2panel = new playerpanel();         p2panel.setlayout(new gridlayout(0,0));         this.boardpanel.add(p2panel);         p2panel.setsize(new dimension(400,250));          boardpanel.setvisible(true);         p1panel.setvisible(true);         p2panel.setvisible(true);     } } 

and code individual button

package view;  import javax.swing.jbutton;  public class positionbutton extends jbutton {          private int x;     private int y;            public positionbutton(int x, int y)     {         this.x = x;         this.y = y;         this.setvisible(true);     }       public int getx()     {         return this.x;     }      public int gety()     {         return this.y;       }                } 

i struggling find out why buttons not appearing. thank in advance.

one major problem code positionbutton overriding getx() , gety(). seems wish use methods determine co-ordinate of button in gridlayout, have name them else. e.g. getxcoord() etc.

try looking on sscce tips.

import java.awt.*; import javax.swing.*; import java.util.arraylist;  public class gameform {      private jframe theframe;     private jpanel boardpanel;     private playerpanel p1panel;     private playerpanel p2panel;      public static void main(string[] args) {         new gameform();     }      public gameform()     {         //set frame         theframe = new jframe();         //set board panel         boardpanel = new jpanel(new borderlayout());         boardpanel.setbackground(color.blue);         theframe.add(this.boardpanel);         //set first players board         p1panel = new playerpanel();         p1panel.setbackground(color.red);         this.boardpanel.add(p1panel);          theframe.setsize(new dimension(800,500));         theframe.setvisible(true);         theframe.setresizable(false);         theframe.setdefaultcloseoperation(jframe.exit_on_close);     } }  class playerpanel extends jpanel {      private java.util.list<positionbutton> buttonlist;      public playerpanel()     {          buttonlist = new arraylist<positionbutton>();         for(int = 0;i < 10;i++)             for(int j = 0;j < 10;j++)             {                 positionbutton pb = new positionbutton(i,j);                 buttonlist.add(pb);                 add(pb);             }     } }  class positionbutton extends jbutton {      private int x;     private int y;      public positionbutton(int x, int y)     {         this.x = x;         this.y = y;         this.setvisible(true);     }       public int getxcoord()     {         return this.x;     }      public int getycoord()     {         return this.y;     } } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -