How do I make the button matrix centered?

0

Good morning, I'm creating a program and I need the matrix to be centered. In what way can I achieve it?

This is my code to make the button array:

  private JPanel getPanel(){
    JButton bMatriz[][] = new JButton[5][5];
    JPanel panel= new JPanel();
    panel.setLayout(new GridLayout(5,5));

    for(int f=0;f<5;f++){
      for(int c=0;c<5;c++){
        bMatriz[f][c] = new JButton(""+f+","+c);
        bMatriz[f][c].setBounds(20,10,360,360);

        panel.setBounds(140,15,270,300);
        panel.add(bMatriz[f][c]); 

      }
    }
    return panel;
  }

Thank you.

    
asked by Roberto Rojas 15.11.2017 в 05:48
source

1 answer

0

By chance you want to do something like this, however I tell you that I modified a touch to make it visible, first I had to extend it from JFrame but I could not show you the example, second instead of a JPanel method, I changed it for a constructor and finally you will see the word this. which refers to the current class, in this case it extends from JFrame or the frame where the JPanel is supported.

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Panel extends JFrame{

    public Panel(){
        JButton bMatriz[][] = new JButton[5][5];

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(5, 5));
        this.add(panel);

        for (int f = 0; f < 5; f++) {
            for (int c = 0; c < 5; c++) {
                bMatriz[f][c] = new JButton("" + f + "," + c);
                bMatriz[f][c].setBounds(20, 10, 360, 360);

                this.setBounds(140, 15, 270, 300);
                panel.setBounds(140, 15, 270, 300);
                panel.add(bMatriz[f][c]);
                panel.setVisible(true);
            }
        }
    }

    public static void main(String[] args) {
        Panel p = new Panel();
        p.setVisible(true);

    }
}

The example I gave you was if you wanted to apply only from a single Java class, however it can be done from two classes, where in one you define the JPanel measures etc, and in the other yes or if you extend JFrame you can add the JPanel or the JPanel you want. Now I show you an example in two Classes the first contains the JPanel and the second extends JFrame which is the frame or window where the JPanel will be supported. (I also prefer everything in a single class)

Defining only the Panel in a class

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

public class Panel{

    public JPanel getPanel(){
        JButton bMatriz[][] = new JButton[5][5];

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(5, 5));


        for (int f = 0; f < 5; f++) {
            for (int c = 0; c < 5; c++) {
                bMatriz[f][c] = new JButton("" + f + "," + c);
                bMatriz[f][c].setBounds(20, 10, 360, 360);

                panel.setBounds(140, 15, 270, 300);
                panel.add(bMatriz[f][c]);
                panel.setVisible(true);
            }
        }
        return panel;
    }

}

Defining the JFrame that will add the JPanels

import javax.swing.JFrame;

public class JFrameMaster extends JFrame {

    public JFrameMaster() {
        Panel p = new Panel();
        this.add(p.getPanel());
        this.setBounds(140, 15, 270, 300);
        this.setVisible(true);
    }

    public static void main(String[] args) {
    /*Acá no es necesario hacer nada más que crearlo, ya que el set Visible está incluido en el constructor*/
        JFrameMaster jfm = new JFrameMaster();
    }
}

Here is an image of how it is ready to walk out

    
answered by 15.11.2017 / 12:58
source