You are adding the label into pnl2
without, it seems, changing the layout. The default layout for JPanel
is FlowLayout
, which uses a centered alignment.
If you want the label to be aligned to the left, you can use a FlowLayout
, making sure the alignment is set to LEADING
:
pnl2.setLayout(new FlowLayout(FlowLayout.LEADING));
pnl2.add(lab3);
Or you can use a different layout, for instance BorderLayout
:
pnl2.setLayout(new BorderLayout());
pnl2.add(lab3);