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

Java Layouts Notes

The document discusses different Java layout managers and collection classes. It describes the FlowLayout, BorderLayout, GridLayout, and CardLayout classes which control component positioning. It also covers the ArrayList and LinkedList classes which provide dynamically resizable lists and their common methods like add(), get(), and remove().

Uploaded by

rajeshchoudhary
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)
48 views

Java Layouts Notes

The document discusses different Java layout managers and collection classes. It describes the FlowLayout, BorderLayout, GridLayout, and CardLayout classes which control component positioning. It also covers the ArrayList and LinkedList classes which provide dynamically resizable lists and their common methods like add(), get(), and remove().

Uploaded by

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

JAVA LAYOUTS NOTES

FlowLayout: FlowLayout arranges components in a left-to-right, top-to-bottom order, with each component
positioned next to the previous one.

import javax.swing.*;

import java.awt.*;

public class FlowLayoutExample {

public static void main(String[] args) {

JFrame frame = new JFrame("FlowLayout Example");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(300, 200);

JPanel panel = new JPanel();

panel.setLayout(new FlowLayout());

panel.add(new JButton("Button 1"));

panel.add(new JButton("Button 2"));

panel.add(new JButton("Button 3"));

frame.add(panel);

frame.setVisible(true);

BorderLayout: BorderLayout divides the container into five regions: North, South, East, West, and Center.
Components are placed in these regions.

import javax.swing.*;

import java.awt.*;

public class BorderLayoutExample {

public static void main(String[] args) {

JFrame frame = new JFrame("BorderLayout Example");


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(300, 200);

frame.setLayout(new BorderLayout());

frame.add(new JButton("North"), BorderLayout.NORTH);

frame.add(new JButton("South"), BorderLayout.SOUTH);

frame.add(new JButton("East"), BorderLayout.EAST);

frame.add(new JButton("West"), BorderLayout.WEST);

frame.add(new JButton("Center"), BorderLayout.CENTER);

frame.setVisible(true);

GridLayout: GridLayout arranges components in a grid of rows and columns. All components are of equal
size.

import javax.swing.*;

import java.awt.*;

public class GridLayoutExample {

public static void main(String[] args) {

JFrame frame = new JFrame("GridLayout Example");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(300, 200);

JPanel panel = new JPanel();

panel.setLayout(new GridLayout(2, 2));

panel.add(new JButton("Button 1"));

panel.add(new JButton("Button 2"));

panel.add(new JButton("Button 3"));


panel.add(new JButton("Button 4"));

frame.add(panel);

frame.setVisible(true);

CardLayout: CardLayout allows you to stack components on top of each other and switch between them, showing
one component at a time.

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class CardLayoutExample {

public static void main(String[] args) {

JFrame frame = new JFrame("CardLayout Example");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(300, 200);

JPanel cardPanel = new JPanel(new CardLayout());

JButton card1 = new JButton("Card 1");

JButton card2 = new JButton("Card 2");

JButton card3 = new JButton("Card 3");

cardPanel.add(card1, "Card 1");

cardPanel.add(card2, "Card 2");

cardPanel.add(card3, "Card 3");

frame.add(cardPanel, BorderLayout.CENTER);

JButton nextButton = new JButton("Next Card");


nextButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{ CardLayout cardLayout = (CardLayout) cardPanel.getLayout();

cardLayout.next(cardPanel);

});

frame.add(nextButton, BorderLayout.SOUTH);

frame.setVisible(true);

}
Java ArrayList

The ArrayList class is a resizable array, which can be found in


the java.util package.

import java.util.ArrayList; // import the ArrayList class

ArrayList<String> cars = new ArrayList<String>();

Add Items

The ArrayList class has many useful methods. For example, to


add elements to the ArrayList, use the add() method:

import java.util.ArrayList;

public class Main {


public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars);
}
}

cars.get(0);

// Import the LinkedList class

import java.util.LinkedList;

public class Main {

public static void main(String[] args) {

LinkedList<String> cars = new LinkedList<String>();


cars.add("Volvo");

cars.add("BMW");

cars.add("Ford");

cars.add("Mazda");

System.out.println(cars);

}
}

Method Description

addFirst() Adds an item to the beginning of the list.

addLast() Add an item to the end of the list

removeFirst() Remove an item from the beginning of the list.

removeLast() Remove an item from the end of the list

getFirst() Get the item at the beginning of the list

getLast() Get the item at the end of the list

You might also like