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

Java_Grid_and_Buttons

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)
5 views2 pages

Java_Grid_and_Buttons

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
You are on page 1/ 2

Java Programs - Grid and Buttons

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

Answer:

import javax.swing.*;

import java.awt.*;

public class GridDemo {

public static void main(String[] args) {

// Create a JFrame

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

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(400, 400);

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

frame.setLayout(new GridLayout(5, 5));

// Add 25 buttons to the frame

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

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

frame.add(button);

// Make the frame visible

frame.setVisible(true);

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

Answer:
import javax.swing.*;

import java.awt.*;

public class NumberButtons {

public static void main(String[] args) {

// Create a JFrame

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

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(300, 300);

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

frame.setLayout(new GridLayout(2, 5));

// Add buttons with numbers from 0 to 9

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

JButton button = new JButton(String.valueOf(i));

frame.add(button);

// Make the frame visible

frame.setVisible(true);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy