JLabel on top of another JLabel

Can I add JLabel on top of another JLabel? Thank you

+5
source share
5 answers

The short answer is yes, because it JLabelis Container, so it can accept Component(a JLabelis a subclass Component) to add in JLabelusing the method add:

JLabel outsideLabel = new JLabel("Hello");
JLabel insideLabel = new JLabel("World");
outsideLabel.add(insideLabel);

In the code above, is insideLabeladded to outsideLabel.

However, a label appears visually with the text "Hello", so you cannot see the label that is contained in the label.

So, the question boils down to what really needs to be done by adding a label on top of another label.


Edit:

From the comments:

, , , , JLabel. 3 , . -, , , , jlabel, 3'mini jlabels ' jlabel. , , ..: |

, , Java.

, Java.

, GridLayout .

JPanel p = new JPanel(new GridLayout(0, 1));
p.add(new JLabel("One"));
p.add(new JLabel("Two"));
p.add(new JLabel("Three"));

JPanel GridLayout , JLabel s.

+7

- , , , .

, , , JLabel , (0, 0), . , , , .

, , , .., .

+1

. ( ) .

0

JLayeredPane .

0

, (hgap, vgap)
JPanel p = new JPanel(new GridLayout(2, 1,-40,0)); //the 40 is the hgap , make it the same with the label height .

0

All Articles