Overlay swing components selectively?
I just sta开发者_C百科rted learning swing, and couldn't find any clear instructions for this: I'd like to have a JLabel (which contains the background image) and, rendered on top of it, two (or perhaps more) JButtons. If I use the layout manager OverlayLayout, the buttons are rendered on top of each other.
What would be the best way to properly render the buttons on top of the label?
- Create a JPanel that contains it's own layout manager (e.g. GridLayout)
- Set the size of the JPanel (Assuming OverlayLayout does not size intelligently)
- Add JButtons to that panel
- Add that panel to the JLabel w/bgimage
A simple solution would be to add them to a JLayeredPane. This way you can control what layer each compoenent is added to.
Depends on your exact requirement. See Background Panel for a couple of suggestions.
精彩评论