启动套接字连接线程并在框架中绘制有关它的信息

发布于 2024-12-17 03:28:50 字数 1072 浏览 1 评论 0原文

我正在尝试编写java程序。

我的想法: 主类启动用于套接字连接的线程,并且主类启动带有GUI的Frame。

package dialogsubsystem2;

import InternetConnection.SocketConnectionThread;

/**
 *
 * @author ACER
 */
public class DialogSubsystemLauncher {

    public SocketConnectionThread connectionSocket = new SocketConnectionThread("192.0.0.100", 2002);

    public DialogSubsystemLauncher() {

        /* Create and display the form */

        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new DialogSubsystemLayoutFrame().setVisible(true);                
            }
        });        

        connectionSocket.start();        
    }


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

       DialogSubsystemLauncher dlgLnch = new DialogSubsystemLauncher();


    }
}

创建的框架根据用户的请求启动另一个框架。

我想在所有框架的状态栏上显示有关套接字连接(来自套接字连接线程)的信息。 但我无法从框架访问套接字连接线程。 我应该如何解决这个问题? 我应该如何启动套接字连接线程才能从任何框架访问它?

I am trying to wirte java-program.

My Idea:
The main class launches thread for socket connection, also the main class launches Frame with GUI.

package dialogsubsystem2;

import InternetConnection.SocketConnectionThread;

/**
 *
 * @author ACER
 */
public class DialogSubsystemLauncher {

    public SocketConnectionThread connectionSocket = new SocketConnectionThread("192.0.0.100", 2002);

    public DialogSubsystemLauncher() {

        /* Create and display the form */

        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new DialogSubsystemLayoutFrame().setVisible(true);                
            }
        });        

        connectionSocket.start();        
    }


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

       DialogSubsystemLauncher dlgLnch = new DialogSubsystemLauncher();


    }
}

The created Frame launches another frames by request from user.

I want to show information about the socket connection (from socket connection thread) on statusbar of all Frames.
But I can't to access the socket connection thread from Frames.
How should I solve this problem?
How should I launch socket connection thread to have access to it from any Frame?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事灯 2024-12-24 03:28:50

您需要其中一个对象具有对另一个对象的引用。我会指出引用线程的框架(因为线程有时可能会被 I/O 阻塞)。

只需让 Frame 的构造函数接受一个线程对象并传递您创建的对象(或向其添加一个 set 方法)。

编辑:

回答评论中的声明。

您需要将引用传递给您创建的其他类(框架)。

public class DialogSubsystemLayoutFrame extends JFrame {
  // You store the reference here.
  private SocketConnectionThread socketConnection = null;

  public DialogSubsystemLayoutFrame(SocketConnectionThread socket) {
    this.socketConnection = socket;
  }
}

从那里,您可以在类中的任何位置使用 this.socketConnection 来对其进行操作。

You need that one of the objects has a reference to the other. I would point to the frame having a refence to the thread (since the thread may be blocked at times by I/O).

Just make the constructor of the Frame accept a thread object and pass the one you created(or add a set method to it).

EDIT:

In answer to aclarations in comments.

It is to the other classes (the Frames) that you create that you need to pass the reference.

public class DialogSubsystemLayoutFrame extends JFrame {
  // You store the reference here.
  private SocketConnectionThread socketConnection = null;

  public DialogSubsystemLayoutFrame(SocketConnectionThread socket) {
    this.socketConnection = socket;
  }
}

From there, you can use this.socketConnection anywhere in the class to operate with it.

遥远的她 2024-12-24 03:28:50

创建您自己的类来扩展 Frame 并在该类中初始化 Socket 线程。

Create your own class that extends the Frame and init the Socket thread in that class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文