.setBounds not working for JLabel and JButton

Don’t use setBounds() to set the size and location of a component.

Let the layout manager do its job. That is if fact what is happening. A JPanel uses a FlowLayout, so the components are being positioned based on the rules of the FlowLayout. You can change the FlowLayout to align components to the left if you want. Or you can use a different layout manager.

Read the Swing tutorial on Layout Managers to find other layout managers you can use.

Leave a Comment