简体   繁体   English

将静态类引用传递给非静态类的构造函数

[英]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.

相关问题 (非静态)内部类的构造方法引用? - Constructor method reference for (non-static) inner class? 带有私有构造函数的非静态类上的NoClassDefFoundError - NoClassDefFoundError on Non-static class with private constructor 对另一个类中的非静态方法进行静态引用 - Making a static reference to a non-static method in another class 如何使用Java 8中的方法引用从非静态类中调用非静态方法? - How to call a non-static method from a non-static class using method reference in Java 8? 静态类非静态成员 - Static Class Non-Static Member 有没有办法显式引用非静态内部类实例? - Is there a way to explicitly reference a non-static inner class instance? 使用所有非静态方法但没有非静态字段的类是否有意义? (或所有静态字段和方法以及构造函数) - Is there a point to having a class with all non-static methods but no non-static fields? (or all static fields and methods along with a constructor) Java:如何从静态嵌套类中引用外部类的非静态字段? - Java: how to reference a non-static field of an outer class from a static nested class? Java - 扩展 static class 非静态 ZA2F2ED4F8EBC2CBB1DZABC - Java - Extended static class of a non-static class Android静态类vs非静态类内存性能 - Android static class vs non-static class memory performance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM