java - JButtons in JPanel only appearing on mousover -


after digging through few other posts have asked question, problem still remains.

here jframe class creates , holds jpanel in turn holds buttons.

import java.awt.gridlayout;  import javax.swing.jframe; import javax.swing.jpanel;  public class window extends jframe{     private static final long serialversionuid = 1l;      private grid grid;     private squarebutton[][] buttons;     private jpanel board;      public window(){         super("territories");          grid=new grid();         buttons=new squarebutton[8][8];         board=new jpanel(new gridlayout(8, 8));          //creates each button proper x , y values , adds each board         (int i=0; i<8; i++){             (int j=0; j<8; j++){                 buttons[j][i]=new squarebutton(j, i);                 board.add(buttons[j][i]);             }         }          //setting frame properties         setsize(560, 560);         setdefaultcloseoperation(exit_on_close);         setresizable(false);          board.setvisible(true);         add(board);          setvisible(true);      } } 

and here jbutton class.

import java.awt.color; import java.awt.dimension;  import javax.swing.borderfactory; import javax.swing.jbutton;  public class squarebutton extends jbutton{     private static final long serialversionuid = 1l;      private int x, y;      public squarebutton(int x, int y){         super();          this.x=x;         this.y=y;          setpreferredsize(new dimension(70, 70));         setborder(borderfactory.createbevelborder(0, color.white, new color(216, 216, 216)));         setbackground(new color(247, 247, 247));          setvisible(true);     }      public int getx(){         return x;     }      public int gety(){         return y;     } } 

it has fact have methods getx() , gety() custom button. every jcomponent (including subclass jbutton) has getx , gety already. when define own in squarebutton, you're overriding original methods, used positioning. i'm not sure order of method calls, layout managers uses these methods determine laying out component.

simply rid of methods if don't need them, or change names.

an aside: don't need calls setvisible on components. calling on frame enough. not aren't adding special functionality button (at least you're showing), may preferred creating instance of jbutton , setting properties, rather creating custom button class.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -