简体   繁体   中英

Can't call UI classes with netbeans GUI builder

Working on learning the GUI builder for netbeans, and I'm pretty confused. I'm writing a basic LoginUI class with the GUI builder. If I run the class as standalone, the window I want pops up as expected. However if I attempt to create a new instance of it, it does not. My flow of control is as follows:

public static void main(String[] args) {
    LoginCntl theLoginCntl = new LoginCntl();
}

public class LoginCntl {

    public LoginCntl(){
        LoginUI theLoginUI = new LoginUI();
}

}

public LoginUI() {
    initComponents();
    System.out.println("Are we here?");
    //This prints out, so I know the program gets here. The problem is the window does not show up here. 
}

 java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new LoginUI().setVisible(true);
        }
    });

I'm really not sure why I can run it standalone and get the window I want then when I create a new instance of it the window does not appear. I get the feeling I'm missing something pretty basic. Any help would be greatly appreciated.

Edit: Added some more of the generated code that netbeans provides. It does call setVisible(). That being said, while looking for setVisible() I noticed that the GUI builder added

public static void main(String args[]) {

in some of its generated code. As far as I'm aware you should only ever have 1 main class in a project. Could this be my problem?

You never Call set visible any where, in fact, it's impossible to determine if your UI actually has a window to be displayed.

If LoginUI extends from something like JFrame , the on you should call ...

theLoginUI.pack();
theLoginUI.setVisible(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM