0% found this document useful (0 votes)
140 views2 pages

Java Swing 5x5 Grid Example

The document provides two Java programs that demonstrate the use of GridLayout in Swing. The first program creates a 5x5 grid of buttons labeled from 1 to 25, while the second program displays buttons labeled with numbers from 0 to 9 in a 2x5 grid. Both programs set up a JFrame and make it visible to the user.

Uploaded by

mayankshinde0116
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views2 pages

Java Swing 5x5 Grid Example

The document provides two Java programs that demonstrate the use of GridLayout in Swing. The first program creates a 5x5 grid of buttons labeled from 1 to 25, while the second program displays buttons labeled with numbers from 0 to 9 in a 2x5 grid. Both programs set up a JFrame and make it visible to the user.

Uploaded by

mayankshinde0116
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programs - Grid and Buttons

Question: Write Java program to demonstrate Grid of 5*5 (Advanced Java Program)

Answer:

import [Link].*;

import [Link].*;

public class GridDemo {

public static void main(String[] args) {

// Create a JFrame

JFrame frame = new JFrame("5x5 Grid Demo");

[Link](JFrame.EXIT_ON_CLOSE);

[Link](400, 400);

// Set layout for the frame as GridLayout with 5 rows and 5 columns

[Link](new GridLayout(5, 5));

// Add 25 buttons to the frame

for (int i = 1; i <= 25; i++) {

JButton button = new JButton("Button " + i);

[Link](button);

// Make the frame visible

[Link](true);

Question: Write a program to display the numbers on buttons from 0 to 9

Answer:
import [Link].*;

import [Link].*;

public class NumberButtons {

public static void main(String[] args) {

// Create a JFrame

JFrame frame = new JFrame("Number Buttons");

[Link](JFrame.EXIT_ON_CLOSE);

[Link](300, 300);

// Set layout for the frame as GridLayout with 2 rows and 5 columns

[Link](new GridLayout(2, 5));

// Add buttons with numbers from 0 to 9

for (int i = 0; i <= 9; i++) {

JButton button = new JButton([Link](i));

[Link](button);

// Make the frame visible

[Link](true);

You might also like