Open JFrame, only after successfull login verification with database. Using Eclipse?

大城市里の小女人 提交于 2019-11-29 17:56:07

Just create a method in your Home class that sets its JFrame to be visible:

public void setJFrameVisible(boolean visible)
{
    frame.setVisible(visible);
}

Then, assuming your instance of your Home class is called "home", all you would have to do is:

home.setJFrameVisible(true);

Let me add a bit more context. When you're extending JFrame, the class inherits all the methods/properties of the JFrame class. That's why when you extend JFrame you can just call obj.setVisible(true), because your class inherited the setVisible method from the JFrame class. What you have is a class that contains a JFrame, so you have to call the setVisible method on the internal JFrame, not the class.

MadProgrammer

Start by defining a simple work flow which allows you to understand the logical path you want your code to take.

Break down the areas of responsibility so that your objects are only do the work that they absolutely have to, for example, your login component should only collect the credentials from the user, it should not be responsible for validating them, that should be the responsibility of some kind of controller.

Equally, neither the login component or it's controller should be responsible for determine what happens after a successful login, that is the responsibility for another controller

This decouples the code and encourages reuse, so the next time you need to present some "login" view, you don't have to recode the whole thing, simply update the controller, model and/or view as required, or re-use it as it is

The concept of a controller in Swing is a little different then a normal MVC implementation, because Swing is already a form of MVC.

What I tend to do instead, is define a contract between the controller and the view which describes what events the view generates (and the events that the controller can expect), for example attemptLogin. This disconnects the controller from the view's implementation, so the view is free to form the view in what ever way it feels like, so long as when it wants to validate the actual credentials it calls attemptLogin

So, you would start with a "main controller" which is responsible for controlling the login and main application controllers. It defines the work flow between the login and the main application and monitors for appropriate events which the controllers may generate to make decisions about what it should do next

A basic flow of operation might look something like

This concept is demonstrated in Java and GUI - Where do ActionListeners belong according to MVC pattern?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!