TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Swing

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 Swing GridLayout

 

—

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/in/java/

Related Posts

Add GUI Components Swing

Java Swing /

Add GUI Components to a Swing Container

JPanel Container Class

Java Swing /

Swing JPanel Container Class

Swing CardLayout

Java Swing /

Java Swing CardLayout

Java Swing FlowLayout

Java Swing /

Java Swing FlowLayout

Java Swing BorderLayout

Java Swing /

Java Swing BorderLayout

‹ Java Swing FlowLayout› Java Swing CardLayout

Recent Posts

  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Print Triangle Pattern Flowchart
  • RAPTOR Editions
  • Flowgorithm Conditional Breakpoint Statement
  • Flowgorithm Read Numbers from File Example
  • Search Text File Flowchart Example
  • Flowgorithm Turtle Graphics Symbols
  • Draw Circle using Flowgorithm Turtle

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version