0% found this document useful (0 votes)
2 views

Practical NO 17

The document contains Java programs demonstrating the use of GridLayout and FlowLayout in creating GUI applications. It includes examples of a 5x5 grid of buttons, buttons displaying numbers from 0 to 9, and a grid layout with specific button arrangements. Each program sets up a JFrame with appropriate layout settings and button configurations.

Uploaded by

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

Practical NO 17

The document contains Java programs demonstrating the use of GridLayout and FlowLayout in creating GUI applications. It includes examples of a 5x5 grid of buttons, buttons displaying numbers from 0 to 9, and a grid layout with specific button arrangements. Each program sets up a JFrame with appropriate layout settings and button configurations.

Uploaded by

rongepratik0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical No:17

Q. 1. Write java Program to Demonstrate Grid of 5* 5


Code:
import java.awt.*;
import javax.swing.*;
public class GridLayoutExample extends JFrame {
public GridLayoutExample() {
// Set GridLayout with 5 rows and 5 columns
setLayout(new GridLayout(5, 5));
// Create buttons and add them to the grid
for (int i = 0; i < 25; i++) {
JButton button = new JButton(String.valueOf(i));
add(button);
}
// Frame settings
setTitle("5x5 Grid Example");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new GridLayoutExample();
}
}
Output:

Q.2. Write a program to display The Number on Button from 0 to 9.


Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NumberButtonsExample extends JFrame {
public NumberButtonsExample() {
// Set Layout
setLayout(new FlowLayout());
// Create buttons with numbers from 0 to 9
for (int i = 0; i < 10; i++) {
JButton button = new JButton(String.valueOf(i));
add(button);
}
// Frame settings
setTitle("Number Buttons Example");
setSize(400, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new NumberButtonsExample();
}
}
Output:

Q.3. Write a program to generate following output

Code:
import java.awt.*;
import javax.swing.*;
public class GridLayoutDemo extends JFrame {
public GridLayoutDemo() {
setTitle("GridLayout Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3, 2, 5, 5)); // 3 rows, 2 columns, 5px horizontal and vertical
add(new JButton("Button 1"));
add(new JButton("Button 2"));
add(new JButton("Button 3"));
add(new JButton("Button 4"));
add(new JButton("Button 5"));
pack(); // Adjusts the frame size to fit the components
setLocationRelativeTo(null); // Centers the frame on the screen
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(GridLayoutDemo::new);
}
}

Output:

You might also like