
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What is the importance of OverlayLayout in Java?
OverlayLayout
- An OverlayLayout is a subclass of Object class and it can arrange the components over the top of each other and uses components specified alignments to position them relatively.
- When different sizes are given to any of the components, we can see all the components.
- To align the components over the other or anywhere in the frame, we can use two methods setAlignmentX() and setAlignmentY(). The parameters are floating values ranging from 0.0f to 1.0f. An OverlayLayout takes the maximum 1.0f by default.
- The important methods of an OverlayLayout are addLayoutComponent(), getTarget(), invalidateLayout(), maximumLayoutSize() and etc.
Example
import java.awt.*; import javax.swing.*; import javax.swing.OverlayLayout; public class OverlayLayoutTest extends JFrame { public OverlayLayoutTest() { setTitle("OverlayLayout Test"); JPanel panel = new JPanel() { public boolean isOptimizedDrawingEnabled() { return false; } }; LayoutManager overlay = new OverlayLayout(panel); panel.setLayout(overlay); JButton button = new JButton("Small"); button.setMaximumSize(new Dimension(75, 50)); button.setBackground(Color.white); panel.add(button); button = new JButton("Medium Btn"); button.setMaximumSize(new Dimension(125, 75)); button.setBackground(Color.lightGray); panel.add(button); button = new JButton("Large Button"); button.setMaximumSize(new Dimension(200, 100)); button.setBackground(Color.orange); panel.add(button); add(panel, BorderLayout.CENTER); setSize(400, 300); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new OverlayLayoutTest(); } }
Output
- Related Articles
- What is the importance of "Java.lang.Class" in Java?
- What is the importance of REPL in Java 9?
- What is the importance of SwingUtilities class in Java?
- What is the importance of JViewport class in Java?
- What is the importance of JSeparator class in Java?
- What is the importance of Runtime class in Java?
- What is the importance of a StringWriter in Java?
- What is the importance of the GridBagConstraints class in Java?
- What is the importance of the CardLayout class in Java?
- What is the importance of the Container class in Java?
- What is the importance of jmod format in Java 9?
- What is the importance of a FocusListener interface in Java?
- What is the importance of a Cursor class in Java?
- What is the importance of Boolean class in Java?\n
- What is the importance of jdeps tool in Java 9?

Advertisements