Hamza Assignment Oop 3
Hamza Assignment Oop 3
OOP
Assignment # 3
Submitted By:
Hamza Nazir
(2872)
Submitted To:
Sir Billal
DATE:12/28/2022
Question :
Department of Computer Sciences
NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
Elaborate different layout managers available in java and also implement all of the
available layouts in separate frames to differentiate them.
Solution
There are several layout managers available in the Java Swing library for organizing and
positioning the components in a container such as a frame or panel. These layout managers
are:
BorderLayout: This layout manager arranges components in five regions: north, south, east,
west, and center. Each region can contain only one component.
GridLayout: This layout manager arranges components in a grid with a specified number of
rows and columns. All components are given equal space.
BoxLayout: This layout manager arranges components in a single row or column. The
components can be aligned vertically or horizontally.
FlowLayout: This layout manager arranges components in a flow, one after the other, in the
order they are added to the container.
CardLayout: This layout manager allows you to display one component at a time from a set
of components. It can be used to create a simple wizard or a tabbed interface.
Sourse code
import java.awt.*;
import javax.swing.*;
// BorderLayout example
JPanel panel1 = new JPanel(new BorderLayout());
panel1.add(new JButton("North"), BorderLayout.NORTH);
panel1.add(new JButton("South"), BorderLayout.SOUTH);
panel1.add(new JButton("East"), BorderLayout.EAST);
panel1.add(new JButton("West"), BorderLayout.WEST);
panel1.add(new JButton("Center"), BorderLayout.CENTER);
frame.add(panel1);
frame.setSize(300, 200);
frame.setVisible(true);
// GridLayout example
JFrame frame2 = new JFrame("GridLayout Example");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel2 = new JPanel(new GridLayout(2, 3));
panel2.add(new JButton("Button 1"));
panel2.add(new JButton("Button 2"));
panel2.add(new JButton("Button 3"));
panel2.add(new JButton("Button 4"));
panel2.add(new JButton("Button 5"));
panel2.add(new JButton("Button 6"));
frame2.add(panel2);
frame2.pack();
frame2.setVisible(true);
// BoxLayout example
JFrame frame3 = new JFrame("BoxLayout Example");
frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel3 = new JPanel();
panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS));
panel3.add(new JButton("Button 1"));
panel3.add(new JButton("Button 2"));
panel3.add(new JButton("Button 3"));
frame3.add(panel3);
frame3.pack();
frame3.setVisible(true);
// FlowLayout example
JFrame frame4 = new JFrame("FlowLayout Example");
frame4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel4 = new JPanel(new FlowLayout());
panel4.add(new JButton("Button 1"));
panel4.add(new JButton("Button 2"));
panel4.add(new JButton("Button 3"));
frame4.add(panel4);
frame4.pack();
frame4.setVisible(true);
// CardLayout example
JFrame frame5 = new JFrame("CardLayout Example");
frame5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel5 = new JPanel(new CardLayout());
panel5.add(new JButton("Button 1"), "Card 1");
panel5.add(new JButton("Button 2"), "Card 2");
panel5.add(new JButton("Button 3"), "Card 3");
frame5.add(panel5);
frame5.pack();
frame5.setVisible(true);
}
}
Out put