Reputation: 41
I'm trying to create multiple labels on a Frame. Every example I've seen has done it exactly how I've done it...
JLabel label1 = new JLabel("Label 1");
JLabel label2 = new JLabel("Label 2");
//... goes on through label5
frame.add(label1);
frame.add(label2);
//... etc through label 5.
Only label 5 is being display. If I comment out 5, only label 4 is display. It's only displaying whatever the last label is.
Upvotes: 0
Views: 189
Reputation: 9365
It depends on which LayoutManager you have set. Probably you left the default BorderLayout
and therefore all of them are added to center and streched to window size. So the last added one - of course - covers all the previously added labels. Try using a FlowLayout
or something else: See this A Visual Guide to Layout Managers
So assuming you are creating the GUI elements within a child of JFrame
, this is how a layout manager is set:
setLayout(new FlowLayout());
//...
add(label1);
add(label2);
add(label3);
//...
Upvotes: 1