簡體   English   中英

將靜態類引用傳遞給非靜態類的構造函數

[英]Pass static class reference to a constructor of non-static class

我需要引用客戶端,因為我需要調用setWinTitle來更改當前窗口的標題。 如何解決?

    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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM