简体   繁体   中英

Pass static class reference to a constructor of non-static class

I need to have a reference to the Client because I need to invoke a setWinTitle to change the title of current window. How to fix it?

    public class Client { 
        public static void main(String[] args){
            JPanel gui= startGUI();
            ...
        }

        private static JPanel startGUI(){
            f = new JFrame();
            JPanel gui = new JPanel(this); // error
        }

        public void setWinTitle(String tite){
            f.setTitle(tite);
        }
    }

public class JPanel extends javax.swing.JPanel {
    Client client;

    public JPanel(Client cl) {
        client= cl; 
        initComponents();
    }
...
}

您需要创建一个Client实例:

JPanel gui = new JPanel(new Client());

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