Matrix in Forms

Asked

Viewed 244 times

3

I’m having trouble creating a matrix within a JPanel in Netbeans, I tried to customize the code by placing within the Netbeans customization code:

           jPanel1.setLayout(new GridLayout(15,15));
           JLabel[][] grid = new JLabel[15][15]; 
           for(int y=0; y<15; y++){
                   for(int x=0; x<15; x++){
                          grid[x][y]=new JLabel("("+x+","+y+")");    
                          jPanel1.add(grid[x][y]);
                   }
           }

It just doesn’t show anything, I wanted to print a matrix in the interface because I need it in the development of a game.

  • Maybe you should put more of your code to understand why not printa nothing, apparently this their code tb was to work, I suggested the Miglayout because it had already made a similar code with him before.

1 answer

4

How did you say the program didn’t printa nothingness I believe it is not a problem with the choice of Layout Manager, must be missing add your Jpanel to your Jframe, so:

setContentPane(jPanel1);

However, it is my suggestion that whenever you are in doubt of which Layoutmanager to use, go to Miglayout, because it is more modern than the others, more flexible and generates less code, making it easier if you need to move the lines of code instead of just dragging the components. Source: Miglayout - Java Layout Manager

Using Miglayout instead of Gridlayout your code would look like this:

jPanel1 = new JPanel();
jPanel1.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(jPanel1);
jPanel1.setLayout(new MigLayout("", "[][]", "[]"));
JLabel[][] grid = new JLabel[15][15];
for(int y=0; y<15; y++){
    for(int x=0; x<15; x++){
        grid[x][y]=new JLabel("("+x+","+y+")");    
        jPanel1.add(grid[x][y], "cell " + x  + " " + y);
    }
}

Upshot:

inserir a descrição da imagem aqui

  • This Miglayout is a library, or a Graphic Interface Design program?

  • @Joãocarlos It is a library, but this is actually transparent to the developer, because you use it just like any other Layout Manager. Look at my code I’m using new MigLayout instead of new GridLayout. Does this code work for you? If not, let me know which version of Java you are using.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.