Practical NO 17
Practical NO 17
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: