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

Hamza Assignment Oop 3

Uploaded by

Hamza Ramay
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)
13 views

Hamza Assignment Oop 3

Uploaded by

Hamza Ramay
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/ 5

NATIONAL UNIVERSITY OF MODERN LANGUAGES

DEPARTMENT OF COMPUTER SCIENCES

OOP
Assignment # 3

Submitted By:
Hamza Nazir
(2872)

Submitted To:
Sir Billal

DATE:12/28/2022

CLASS: BSCS-40B (2nd Semester) Evening

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.*;

public class LayoutExample {


public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("Layout Examples");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// 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);

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

// 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

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

Explanation of the code:


The LayoutExamples class creates five separate frames, each with a different layout manager.
The main method is the entry point of the program and is where the frames are created and
displayed.
The first frame is created using a BorderLayout layout manager. A JPanel is created with a
BorderLayout and five buttons are added to it, each in a different region of the layout (north,
south, east, west, and center). The panel is then added to the frame and the frame is made
visible.
The second frame is created using a GridLayout layout manager. A JPanel is created with a
GridLayout that has 2 rows and 3 columns. Six buttons are added to the panel, which are
placed in the grid according to the layout. The panel is then added to the frame and the frame
is made visible.
The third frame is created using a BoxLayout layout manager. A JPanel is created and its
layout is set to a BoxLayout that arranges components vertically. Three buttons are added to
the panel and the panel is added to the frame. The frame is made visible.
The fourth frame is created using a FlowLayout layout manager. A JPanel is created with a
FlowLayout and three buttons are added to it. The panel is then added to the frame and the
frame is made visible.
The fifth frame is created using a CardLayout layout manager. A JPanel is created with a
CardLayout and three buttons are added to it, each with a different name ("Card 1", "Card 2",
"Card 3"). The panel is then added to the frame and the frame is made visible. The
CardLayout allows you to switch between the different buttons (cards) using the show
method.

Department of Computer Sciences


NATIONAL UNIVERSITY OF MODERN LANGUAGES

You might also like