• We just launched and are currently in beta. Join us as we build and grow the community.

OverlayLayout as Layout Manager in Java

THiMoLeoNe

Grin Generator
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
84
Likes
130
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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:

over.png


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. public

    class

    overlayLayout {
  4. public

    static

    void

    main(

    String

    args[

    ]

    )

    {
  5. JFrame

    frame =

    new

    JFrame

    (

    "OverlayLayout as LayoutManager"

    )

    ;
  6. JPanel

    panel =

    new

    JPanel

    (

    )

    ;
  7. OverlayLayout

    overlay =

    new

    OverlayLayout

    (

    panel)

    ;
  8. JButton

    button =

    new

    JButton

    (

    "Small"

    )

    ;

  9. button.setMaximumSize

    (

    new

    Dimension

    (

    100

    , 100

    )

    )

    ;
  10. button.setBackground

    (

    Color

    .red

    )

    ;
  11. panel.add

    (

    button)

    ;

  12. button =

    new

    JButton

    (

    "Medium"

    )

    ;
  13. button.setMaximumSize

    (

    new

    Dimension

    (

    200

    , 200

    )

    )

    ;
  14. button.setBackground

    (

    Color

    .blue

    )

    ;
  15. panel.add

    (

    button)

    ;

  16. button =

    new

    JButton

    (

    "Large"

    )

    ;
  17. button.setMaximumSize

    (

    new

    Dimension

    (

    300

    , 300

    )

    )

    ;
  18. button.setBackground

    (

    Color

    .green

    )

    ;
  19. panel.add

    (

    button)

    ;

  20. panel.setLayout

    (

    overlay)

    ;

  21. frame.getContentPane

    (

    )

    .add

    (

    panel, "Center"

    )

    ;
  22. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  23. frame.setSize

    (

    400

    , 300

    )

    ;
  24. frame.setVisible

    (

    true

    )

    ;
  25. }
  26. }

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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,491

323,080

323,089

Top