Java Layouts Notes
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.*;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
panel.setLayout(new FlowLayout());
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.*;
frame.setSize(300, 200);
frame.setLayout(new BorderLayout());
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.*;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
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;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.add(cardPanel, BorderLayout.CENTER);
cardLayout.next(cardPanel);
});
frame.add(nextButton, BorderLayout.SOUTH);
frame.setVisible(true);
}
Java ArrayList
Add Items
import java.util.ArrayList;
cars.get(0);
import java.util.LinkedList;
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars);
}
}
Method Description