I have this form :

which has one button,three labels,three textfields and internal frame above them.
this is going to be a Nurikabe game, the user will input matrix size msize for short,number of numbered cells(cells that contain a number)nncells for short, and a maximum number a numbered cell can have max for short. 
I create a grid of msize*msize and fill nncells of it, which cells I choose ? well I choose them randomly, what number do I fill them with ? a random number in the interval [1,max].
I created this form with Netbeans Designer, this is the code for the whole JFrame file: package javaapplication1;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class PrologTest extends javax.swing.JFrame {
public PrologTest() {
    initComponents();
}
private void initComponents()
{
    jLabel1 = new javax.swing.JLabel();
    matrixsize = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    numcells = new javax.swing.JTextField();
    jLabel2 = new javax.swing.JLabel();
    maximumnumber = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    jButton3 = new javax.swing.JButton();
    Nurikabe = new javax.swing.JInternalFrame();
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBackground(new java.awt.Color(255, 0, 0));
    jLabel1.setText("matrix size");
    jLabel1.setToolTipText("");
    matrixsize.setToolTipText("");
    matrixsize.setName("matrixsize"); // NOI18N
    jButton1.setBackground(new java.awt.Color(255, 255, 255));
    jButton1.setText("validate");
    jButton1.setToolTipText("");
    jButton2.setText("show solutions");
    jButton2.setToolTipText("");
    numcells.setToolTipText("");
    numcells.setName("numcells"); // NOI18N
    jLabel2.setText("number of numbered cells");
    jLabel2.setToolTipText("");
    maximumnumber.setToolTipText("");
    maximumnumber.setName("maximumnumber"); // NOI18N
    jLabel3.setText("maximum number for a cell");
    jLabel3.setToolTipText("");
    jButton3.setText("create");
    jButton3.setToolTipText("");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton3ActionPerformed(evt);
        }
    });
    Nurikabe.setVisible(true);
    Nurikabe.getContentPane().setLayout(new java.awt.GridLayout(1, 0));
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(numcells)
                .addComponent(maximumnumber, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                .addComponent(matrixsize))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel3))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE))
            .addContainerGap())
        .addComponent(Nurikabe)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addComponent(Nurikabe, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(matrixsize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton3))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton2)
                    .addGap(37, 37, 37))
                .addGroup(layout.createSequentialGroup()
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(numcells, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(maximumnumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
    );
    pack();
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         
    Nurikabe.removeAll();
    int matsize=Integer.parseInt(matrixsize.getText());
    int numberedcells=Integer.parseInt(numcells.getText());
    int maximumcellnum=Integer.parseInt(maximumnumber.getText());
    Random r=new Random();           
    Cells=new JButton[matsize][matsize];
    Nurikabe.getContentPane().setLayout(new GridLayout(matsize,matsize));            
    for(int i=0;i<matsize;i++)                            
    {
        for(int j=0;j<matsize;j++)    
        {
            Cells[i][j]=new JButton();
            Cells[i][j].setContentAreaFilled(false);  
            Cells[i][j].setBackground(Color.white);
            final JButton Cell=Cells[i][j];
            Cells[i][j].addMouseListener(new java.awt.event.MouseAdapter() 
            {
                @Override
                public void mousePressed(java.awt.event.MouseEvent evt) 
                {
                    if(SwingUtilities.isRightMouseButton(evt))
                    {
                        Cell.setBackground(Color.green);
                    }
                    else if(SwingUtilities.isLeftMouseButton(evt))
                    {
                        Cell.setBackground(Color.blue);
                    }                                 
                }
            });  
            Nurikabe.add(Cells[i][j]);
        }
    }
    for(int i=0;i<numberedcells;i++)    
    {
        int k=r.nextInt(matsize-1)+1,l=r.nextInt(matsize-1)+1;
        String text=String.valueOf(r.nextInt(maximumcellnum));
        Cells[k][l].setText(text);
        Cells[k][l].setBackground(Color.green);                
    }            
    Nurikabe.validate();
    Nurikabe.pack();
    Nurikabe.repaint();                
}
public static void main(String[] args)
{
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(PrologTest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }                
    java.awt.EventQueue.invokeLater(() -> {
        new PrologTest().setVisible(true);
    });
}
private javax.swing.JButton Cells[][];
// Variables declaration - do not modify                     
private javax.swing.JInternalFrame Nurikabe;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField matrixsize;
private javax.swing.JTextField maximumnumber;
private javax.swing.JTextField numcells;
}
this code doesn't work !! it doesn't show a thing in the internal frame.
I'm removing all compenents So to clear what was added by previous press, I called validate and repaint in the end.
I found this question on SO but The answer work when I know the size of the grid, I don't know it as the user will input it.(also didn't fi
So what's the problem ?
PS
when I press the button more than one time the internal frame gets bigger but with nothing in it, I set the layout of it to null from netbeans and set layout of it in the button.
 
     
    