OverlayLayout as Layout Manager in Java

This tutorial is about the OverlayLayout as Layout Manager in Java. An OverlayLayout is a layout where it arranges components over the top of each other or behind 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:
  1. import java.awt.*; // used to access Color and Dimension class
  2. 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.
  1. JFrame frame = new JFrame("OverlayLayout as LayoutManager");
  2. JPanel panel = new JPanel();
  3. OverlayLayout overlay = new OverlayLayout(panel);
  4. 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 create the first button. To set its size we will use the setMaximumSize method with the dimension class that has the width and height of the button. Set also the background color of the button by using the setBackground method. Lastly add the button using the add method of the panel. Note that this button is for the button labeled as "Small".
  1. button.setMaximumSize(new Dimension(100, 100));
  2. button.setBackground(Color.red);
  3. panel.add(button);
To create another button from the variable of the JButton named button, we will instantiate again the button but has different label, namely "Medium". Have all its size, background color, and add the button to the panel.
  1. button = new JButton("Medium");
  2. button.setMaximumSize(new Dimension(200, 200));
  3. button.setBackground(Color.blue);
  4. panel.add(button);
Repeat the instruction to create other button and name this new button as "Large".
  1. button = new JButton("Large");
  2. button.setMaximumSize(new Dimension(300, 300));
  3. button.setBackground(Color.green);
  4. panel.add(button);
Now, set the OverlayLayout as your Layout Manager in the panel not on the frame.
  1. 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. Have this code below:
  1. frame.getContentPane().add(panel, "Center");
  2. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  3. frame.setSize(400, 300);
  4. frame.setVisible(true);
Output: output Here's the full code of this tutorial:
  1. import java.awt.*; // used to access Color and Dimension class
  2. import javax.swing.*; //used to access JButton,JFrame,JPanel, and OverlayLayout class
  3.  
  4.  
  5. public class overlayLayout {
  6. public static void main(String args[]) {
  7. JFrame frame = new JFrame("OverlayLayout as LayoutManager");
  8. JPanel panel = new JPanel();
  9. OverlayLayout overlay = new OverlayLayout(panel);
  10. JButton button = new JButton("Small");
  11.  
  12. button.setMaximumSize(new Dimension(100, 100));
  13. button.setBackground(Color.red);
  14. panel.add(button);
  15.  
  16. button = new JButton("Medium");
  17. button.setMaximumSize(new Dimension(200, 200));
  18. button.setBackground(Color.blue);
  19. panel.add(button);
  20.  
  21. button = new JButton("Large");
  22. button.setMaximumSize(new Dimension(300, 300));
  23. button.setBackground(Color.green);
  24. panel.add(button);
  25.  
  26. panel.setLayout(overlay);
  27.  
  28. frame.getContentPane().add(panel, "Center");
  29. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30. frame.setSize(400, 300);
  31. frame.setVisible(true);
  32. }
  33. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment