OverlayLayout as Layout Manager in Java

May 28, 2020
Tutorials
overlay layout in java

This tutorial is about the OverlayLayout as Layout Manager in Java. An OverlayLayout is a layout wherein it arranges components over the top of each other or behind from each other.

So, now let’s start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of overlayLayout.java.

2. Import following package library:

import java.awt.*; // used to access Color and Dimension class
import javax.swing.*; //used to access JButton,JFrame,JPanel, and OverlayLayout class

3. Initialize your variables in your Main, variable frame for JFrame, variable panel as JPanel, variable overlay for OverlayLayout and put inside the JPanel on it, and variable button labeled Small as JButton as we will create buttons on the top of each other.

JFrame frame = new JFrame("OverlayLayout as LayoutManager");
JPanel panel = new JPanel();	
OverlayLayout overlay = new OverlayLayout(panel);
JButton button = new JButton("Small");

 

4. Now, we will create three buttons using a single variable for the JButton. That is to have these buttons to be in top of each other.

We will make the first button. To set its size, we will use the setMaximumSize method with the measurement class that has the width and height of the button. Set the background color of the button by utilizing the setBackground method. Finally, add the button using the add method of the panel. Note that this button is named as “Small”.

button.setMaximumSize(new Dimension(100, 100));
button.setBackground(Color.red);
panel.add(button);

 

To make another button from the variable of the JButton named button, we will instantiate again the button but has a different label, namely “Medium“. Have all its size, background color, and add the button to the panel.

button = new JButton("Medium");
button.setMaximumSize(new Dimension(200, 200));
button.setBackground(Color.blue);
panel.add(button);

 

Repeat the instruction to create other button and name this new button as “Large“.

button = new JButton("Large");
button.setMaximumSize(new Dimension(300, 300));
button.setBackground(Color.green);
panel.add(button);

 

Now, set the OverlayLayout as your Layout Manager in the panel, not on the frame.

panel.setLayout(overlay);

5. Lastly, add the panel to the frame with a default BorderLayout of Center position, set its size and visibility, and its close operation.
Copy this code below:

frame.getContentPane().add(panel, "Center");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);

 

Output:

output


Here’s the full code of this tutorial:

import java.awt.*; // used to access Color and Dimension class
import javax.swing.*; //used to access JButton,JFrame,JPanel, and OverlayLayout class
 
 
public class overlayLayout {
  public static void main(String args[]) {
    JFrame frame = new JFrame("OverlayLayout as LayoutManager");
    JPanel panel = new JPanel();	
    OverlayLayout overlay = new OverlayLayout(panel);
    JButton button = new JButton("Small");
 
    button.setMaximumSize(new Dimension(100, 100));
    button.setBackground(Color.red);
    panel.add(button);
 
    button = new JButton("Medium");
    button.setMaximumSize(new Dimension(200, 200));
    button.setBackground(Color.blue);
    panel.add(button);
 
    button = new JButton("Large");
    button.setMaximumSize(new Dimension(300, 300));
    button.setBackground(Color.green);
    panel.add(button);    
 
    panel.setLayout(overlay);
 
    frame.getContentPane().add(panel, "Center");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);
  }
}

 

 

Facebook Comments

Leave a Reply

Your email address will not be published.