[英]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. 我需要引用客户端,因为我需要调用
setWinTitle
来更改当前窗口的标题。 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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.