我使用单例模式来构建一个类,但它确实接收了另一个单例作为参数,它不起作用,这真的可能吗?
这是整个项目:https://github.com/raphaelbgr/SwingSocketClient/tree/master/src
package clientmain;
import gui.janelas.JanelaMain;
public class ClientMain {
public static int port = 0;
public static Thread receiver = null;
public static String ip = null;
public static String your_name = null;
public static void main(String[] args) {
JanelaMain jam = JanelaMain.getInstance(); //JanelaMain extends JFrame
jam.setLocationByPlatform(false);
}
这是它的单身人士。
//SINGLETON PATTERN BLOCK
private static JanelaMain jam;
public static JanelaMain getInstance() {
if (JanelaMain.jam == null) {
jam = new JanelaMain(JanelaSelectServer.getInstance()); //Another one passed by parameter
}
return jam;
}
另一个类Singleton块:
//SINGLETON PATTERN BLOCK
private JanelaSelectServer() {} //JanelaSelectServer extends another JFrame
private static JanelaSelectServer jsv;
public static JanelaSelectServer getInstance() {
if (jsv == null) {
jsv = new JanelaSelectServer();
}
return jsv;
}
狗屎发生(A Loop)
s.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
at gui.janelas.JanelaMain.getInstance(JanelaMain.java:144)
at gui.updatelogs.ConnectionLog.<init>(ConnectionLog.java:13)
at gui.updatelogs.ConnectionLog.getInstance(ConnectionLog.java:34)
at gui.janelas.JanelaMain.<init>(JanelaMain.java:37)
感谢。
答案 0 :(得分:3)
你的代码看起来有点奇怪,为什么要给另一个单身人士?您可以在需要的地方使用单例类,而无需将其提供给另一个类。
答案 1 :(得分:2)
问题不是关于那些类,而是关于ConnectionLog类。您正尝试在JanelaMain中创建实例,ConnectionLog尝试创建JanelaMain的实例。
@SuppressWarnings("serial")
public class JanelaMain extends JFrame {
private FlowLayout layout = new FlowLayout();
private JTextField jtxt_send = null;
//CUSTOM SWING COMPONENTS
private ConnectionLog cn_log = ConnectionLog.getInstance();
private TextLog msg_list = TextLog.getInstance();
@SuppressWarnings("serial")
public class ConnectionLog extends JTextField {
private JanelaMain jam = JanelaMain.getInstance();