从另一个类访问Java Swing GUI

时间:2023-01-27 13:23:11

I have three classes - class 1 which creates an instance of class 2 (the GUI), class 2 which implements the GUI and class 3 which tries to update the GUI.

我有三个类 - 类1创建类2(GUI)的实例,类2实现GUI和类3尝试更新GUI。

In class 1 I create the GUI like this:

在第1类中,我创建了这样的GUI:

s = new ServerSocket(6067);         

//while(true){                              

    Socket ClientSocket = s.accept();   // Accept connections   

    // Create instance of the GUI (class 2) on a new thread     
    work w = cpd.new work();

    Thread t = new Thread(w);
    t.start();


    // Create instance of class 3 that uses the GUI on a new thread             
    Charger cpt = new ChargingPoint(ClientSocket, w.gui);   
    cpt.start();                                                
//}


class work implements Runnable{

    GUI gui;

    public void run(){

        try{

            gui = new GUI();
            gui.setVisible(true);                   
        }

        catch(Exception e){}

    }
}

In class 2 I implement the GUI and have some methods that allow it to be updated such as this one:

在第2课我实现GUI并有一些方法允许它更新,如下所示:

public void updateConsole(String text){
    Console.append(text + "\n");
}

In class 3 I try to use these methods like below but the code gets stuck when it reaches a statement like this:

在第3类中,我尝试使用如下所示的这些方法,但代码在达到如下语句时会卡住:

gui.updateConsole("Data: " + data);

This has worked for me before when I had the GUI class (class 2) as the main class. I passed an instance of this to all the other classes and they could us this to update the GUI without any issues. This time however I am creating the GUI from another class (class 1) and this method is no longer working and I'm struggling to work out why.

在我将GUI类(第2类)作为主类之前,这对我有用。我将此实例传递给所有其他类,他们可以让我们更新GUI而不会出现任何问题。但是这次我从另一个类(类1)创建GUI,这个方法不再有效,我正在努力找出原因。

Any suggestions would be very much appreciated.

任何建议将非常感谢。

1 个解决方案

#1


0  

I'll try to give you the tools to debug this yourself.

我会尝试给你自己调试的工具。

Try using eclipse or similar debugger. http://www.vogella.com/articles/EclipseDebugging/article.html

尝试使用eclipse或类似的调试器。 http://www.vogella.com/articles/EclipseDebugging/article.html

Start by checking that class2 is actually calling back to the correct instance of the gui class.

首先检查class2实际上是否正在回调gui类的正确实例。

With the debugger, you can find an 'object id'. This will tell you the unique id of the object in memory. It is usually the memory pointer. Check that the id in ChargingPoint matches the id created in Work class. Running methods on the wrong class is the most common mistake with callback.

使用调试器,您可以找到“对象ID”。这将告诉您内存中对象的唯一ID。它通常是内存指针。检查ChargingPoint中的id是否与Work类中创建的id匹配。在错误的类上运行方法是回调最常见的错误。

Then move onto putting a break point in your gui.updateConsole and step into the code.

然后转到gui.updateConsole中的断点并进入代码。

This should quickly reveal the issue.

这应该很快揭示出这个问题。

Regards

#1


0  

I'll try to give you the tools to debug this yourself.

我会尝试给你自己调试的工具。

Try using eclipse or similar debugger. http://www.vogella.com/articles/EclipseDebugging/article.html

尝试使用eclipse或类似的调试器。 http://www.vogella.com/articles/EclipseDebugging/article.html

Start by checking that class2 is actually calling back to the correct instance of the gui class.

首先检查class2实际上是否正在回调gui类的正确实例。

With the debugger, you can find an 'object id'. This will tell you the unique id of the object in memory. It is usually the memory pointer. Check that the id in ChargingPoint matches the id created in Work class. Running methods on the wrong class is the most common mistake with callback.

使用调试器,您可以找到“对象ID”。这将告诉您内存中对象的唯一ID。它通常是内存指针。检查ChargingPoint中的id是否与Work类中创建的id匹配。在错误的类上运行方法是回调最常见的错误。

Then move onto putting a break point in your gui.updateConsole and step into the code.

然后转到gui.updateConsole中的断点并进入代码。

This should quickly reveal the issue.

这应该很快揭示出这个问题。

Regards