Java Swing GridLayout
Overview
In this tutorial, we will learn about Java Swing GridLayout. GridLayout uses grid cells to place the components.
Java Swing GridLayout
In GridLayout, each cell has the same size and each GUI component takes up the same space in a container. When the user resizes the container, the size of the GUI component also changes.
Java Demo Program
/**************************************** * * Filename: GridLayoutDemo.java * Java Tutorials - www.TestingDocs.com * *****************************************/ import java.awt.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class GridLayoutDemo { // main method public static void main(String[] args) { // Create a Frame JFrame frame = new JFrame("GridLayout -www.TestingDocs.com"); JButton btn0 = new JButton("0"); JButton btn1 = new JButton("1"); JButton btn2 = new JButton("2"); JButton btn3 = new JButton("3"); JButton btn4 = new JButton("4"); JButton btn5 = new JButton("5"); JButton btn6 = new JButton("6"); JButton btn7 = new JButton("7"); JButton btn8 = new JButton("8"); JButton btn9 = new JButton("9"); JButton btnC = new JButton("C"); JButton btnEquals = new JButton("="); // Create grid layout :with 4 rows , 3 columns JPanel panel = new JPanel(new GridLayout(4,3,10,10)); // add buttons panel.add(btn0); panel.add(btn1); panel.add(btn2); panel.add(btn3); panel.add(btn4); panel.add(btn5); panel.add(btn6); panel.add(btn7); panel.add(btn8); panel.add(btn9); panel.add(btnC); panel.add(btnEquals); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600,400); frame.getContentPane().add(panel); frame.setVisible(true); } }
Output
Run the Java application to view the output.
—
Java Tutorial on this website:
https://www.testingdocs.com/java-tutorial/
For more information on Java, visit the official website :