As you know, containers (such as JFrame, JPanel, ...) are used to hold controls in them, but they do not have or default to arrange objects that are not what we want. So we need to use layouts to do this. In other words, Layouts allow us to arrange the controls in the containers in a logical and beautiful way.
Jpanel gridLayout java is a simple and easy to use layout that divides our container into a table of rows and columns of equal size.
We use Jpanel gridLayout java when our layout is like a table, such as the layout of the JButton in a calculator like this.
File name: MyGridLayout.java
Continue MyGridLayout.java
Continue MyGridLayout.java
The output:
- GridLayout (int rows, int cols): Creates GridLayout with the specified row and column numbers.
- GridLayout (int rows, int cols, int hgap, int vgap): Creates a GridLayout with the number of rows, the number of columns, the spacing between rows and columns.
In this code you notice the command gridLayout.layoutContainer (mainPanel) ; which allows the container (in this case, mainPanel) to use that layout (here is gridLayout).
More about how to create java swing jpanel with detail examples:
Jpanel gridLayout java is a simple and easy to use layout that divides our container into a table of rows and columns of equal size.
We use Jpanel gridLayout java when our layout is like a table, such as the layout of the JButton in a calculator like this.
The example of using GridLayout
In this case, we will create a table of 4 Jbutton placed in a JPanel using GridLayout. When clicking on each JButton, you can change the horizontal distance between them. Below there are 2 JLabels showing the horizontal distance (hGap), vertical (vGap) between them.File name: MyGridLayout.java
Continue MyGridLayout.java
Continue MyGridLayout.java
The output:
Jpanel gridLayout java initialization methods
- GridLayout (): Create Gridlayout with 1 row, 1 column- GridLayout (int rows, int cols): Creates GridLayout with the specified row and column numbers.
- GridLayout (int rows, int cols, int hgap, int vgap): Creates a GridLayout with the number of rows, the number of columns, the spacing between rows and columns.
In this code you notice the command gridLayout.layoutContainer (mainPanel) ; which allows the container (in this case, mainPanel) to use that layout (here is gridLayout).
More about how to create java swing jpanel with detail examples:
Comments
Post a Comment