java - Filling Frame with Buttons Using GridBagLayout -


i trying build matching game icons attached each button. although, isn't close being finished, have problem filling panel buttons.

with code grey colored frame. if comment out 3 methods use under "//execution" panel black (which how testing see if buttons filling space or not.)

for reaso,n buttons aren't being populated onto panel.
need help!!! going wrong?

import java.awt.color; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.borderfactory; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel;  public class memorymainframe extends jframe implements actionlistener {      jbutton button[] = new jbutton[16];     private static final int sixteen_buttons = 16;     jpanel mainpanel = new jpanel();     double dim = math.sqrt(sixteen_buttons);     int numofcolumns = (int) (dim);     int numofrows = (int) (dim);      public static void main(string[] args) {         new memorymainframe();     }      public memorymainframe() {          this.settitle("memorygame!!!");         this.setsize(400, 400);         this.setlocationrelativeto(null);         this.setdefaultcloseoperation(exit_on_close);         this.setvisible(true);          mainpanel.setbackground(color.black);         mainpanel.setlayout(new gridbaglayout());          // execution         fillbuttonarray(sixteen_buttons);         addbuttonlistener(sixteen_buttons);         buttontopanel(sixteen_buttons);          this.add(mainpanel);     }      public void fillbuttonarray(int numofbuttons) {         int = 0;          while (i < numofbuttons) {             button[i] = new jbutton("asdf");         }     }      public void addbuttonlistener(int numofbuttons) {         int = 0;          while (i < numofbuttons) {             button[i].addactionlistener(this);         }     }      public void buttontopanel(int numofbuttons) {         int n = 0;         gridbagconstraints gbc = new gridbagconstraints();          (int = 0; < numofcolumns; i++) {             (int j = 0; j < numofrows; j++) {                  gbc.gridx = i;                 gbc.gridy = j;                 n++;                  button[n].setborder(borderfactory.createlineborder(                         color.dark_gray, 2));                 mainpanel.add(button[n]);             }         }     }      public void actionperformed(actionevent arg0) {          jframe j = new jframe();         j.setsize(300, 300);         j.setvisible(true);     } } 

i use "asdf" test see if buttons work well.

also, actionperformed test well. part of code irrelevant.

you're creating gridbagconstraints you're not using them.

change this:

mainpanel.add(button[n]); 

to this:

// passes both component , gbc container mainpanel.add(button[n], gbc);  

edit
you've got never-ending loop here:

  while (i < numofbuttons) {      button[i] = new jbutton("asdf");   } 

and likewise addbuttonlistener(...) method.

you'll want fix using either loop or else changing i within loop.

also per andrew thompson's comment, you're setting jframe visible early, before components have been added.

also use of math.sqrt casting result int risky , risks getting unexpected results. declare side length 8 , square int if need to.

for example of gridbaglayout, please check out:

import java.awt.color; import java.awt.component; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; import java.awt.event.actionevent; import javax.swing.*;  @suppresswarnings("serial")  // avoid extending jframe if possible public class memorymainpanel extends jpanel {    private static final int rows = 8;    private static final color background = color.black;    private static final int i_gap = 5;    private static final insets btn_insets = new insets(i_gap, i_gap, i_gap, i_gap);    private jbutton[][] buttons = new jbutton[rows][rows];      public memorymainpanel() {       setbackground(background);       setlayout(new gridbaglayout());       (int row = 0; row < buttons.length; row++) {          (int col = 0; col < buttons[row].length; col++) {             jbutton btn = new jbutton(new buttonaction(row, col));             add(btn, creategbc(row, col));             buttons[row][col] = btn;          }       }    }     private gridbagconstraints creategbc(int y, int x) {       gridbagconstraints gbc = new gridbagconstraints();       gbc.gridx = x;       gbc.gridy = y;       gbc.weightx = 1.0;       gbc.weighty = 1.0;       gbc.insets = btn_insets;       return gbc;    }     private class buttonaction extends abstractaction {       private int row;       private int col;        public buttonaction(int row, int col) {          super("asdf");          this.row = row;          this.col = col;       }        @override       public void actionperformed(actionevent e) {          string text = string.format("column, row: [%d, %d]", col + 1, row + 1);          component parentcomponent = memorymainpanel.this;          string message = text;          string title = "button pressed";          int messagetype = joptionpane.plain_message;          icon icon = null;          joptionpane.showmessagedialog(parentcomponent, message, title, messagetype, icon);       }    }     private static void createandshowgui() {       memorymainpanel mainpanel = new memorymainpanel();        jframe frame = new jframe("memorymainpanel");       frame.setdefaultcloseoperation(jframe.dispose_on_close);       frame.getcontentpane().add(mainpanel);       frame.pack();       frame.setlocationrelativeto(null);       frame.setvisible(true);    }     public static void main(string[] args) {       swingutilities.invokelater(new runnable() {          public void run() {             createandshowgui();          }       });    } } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -