启动套接字连接线程并在框架中绘制有关它的信息
我正在尝试编写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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要其中一个对象具有对另一个对象的引用。我会指出引用线程的框架(因为线程有时可能会被 I/O 阻塞)。
只需让 Frame 的构造函数接受一个线程对象并传递您创建的对象(或向其添加一个 set 方法)。
编辑:
回答评论中的声明。
您需要将引用传递给您创建的其他类(框架)。
从那里,您可以在类中的任何位置使用
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.
From there, you can use
this.socketConnection
anywhere in the class to operate with it.创建您自己的类来扩展 Frame 并在该类中初始化 Socket 线程。
Create your own class that extends the Frame and init the Socket thread in that class.