Using swing GUI in java

1- I have a code that is basically a user class with name and number
attributes which I plan to use as a login page, so what I did was
create a new GUI class for this user class completely separate from
the user class (as in one class for functions and another for the GUI)
, was this wrong? as in should I have placed the user methods and the
GUI in one class?

A common concept in UI development is “model-view-controller” (don’t worry about controller just yet). This means that your “data” is modelled in some way (ie User) and your “view” (UI) takes that model and makes decisions about how the model should be presented to the user. It also helps manage the interaction between the user and the model.

So, yes, keeping your “data” independent from your “ui” is the right strategy. Always remember, it’s the UI’s responsibility to determine “how” the data is formatted, the model is just a means to manage the data in some meaningful way.

2- I have 2 GUIs, a welcome GUI and a login GUI, how do I ensure that
one leads to the next? I tried to make the welcome frame invisible(the
login frame is made visible in its own GUI) but that didn’t work

This is a little broader in concept. Typically, we might recommend using a CardLayout to “flip” between views, but this would assume you want to revisit those views at some point.

On a more “abstract” point of view, you would use some kind of “controller” to make decisions about what should happen based on the current state.

This means, if “welcome” has not been presented, you’d present the “welcome” view. When the user is ready to move beyond it, “welcome” would notify the “controller” and the controller would then decide what to do next.

ie. Do you have previously saved credentials or not? If so, you could auto login the user and move on, otherwise you’d need to present the “login” view in order to get the credentials and allow the controller(s) to authenticate the user.

This moves you onto the “observer pattern” (aka listeners), where an interest party registers interest in been notified when some state has changed.

When trying to design these kind of systems, always be asking yourself some basic questions

  • Just how much do I need to expose to other parts of the program? ie You’re welcome view doesn’t need to know about the login view, as it could do things to the login view which are out side of it’s scope of responsibility
  • How hard or how much work would I need to do to change any part of it?! So you get the welcome screen to open the login screen, but now you want to add in “auto login”, just how much work are you going to have to go to make that work? Would it have been easier if the welcome and login views were independent of each other and controlled through some other mechanism?

Take a look at Java and GUI – Where do ActionListeners belong according to MVC pattern? for simple implementation example