- Avoid using
setSize(...)
orsetPreferredSize(...)
if possible. - Instead let the components and their layout managers set their own sizes.
- Use a CardLayout to swap views instead of what you're doing. If you do this, the CardLayout will size its container to fit all the "cards" that it has been given.
- Don't forget to call
pack()
on the GUI after adding all components - Don't forget to call
setVisible(true)
after adding all components and after calling pack. - When creating new JTextFields and JPasswordFields, pass in an int for the number of columns into the constructors.
Edit
You ask:
whats pack() used for?
The pack()
method tells the GUI to have all the layout managers of its constituent containers to lay out their components, and then to set the best size of the GUI after every component has been properly placed.