如何在另一个对象的 JTextArea 中显示一个对象的数据?
我有一个名为 handler 的类,该类处理来自浏览器的 http 请求,我想在另一个名为 HttpHeadersFrame 的类的 JTextArea 中显示请求的 http 标头!这就是我尝试过的,
public class Handler
{
HttpHeadersFrame headersFrame; //This frame contains JTextArea component
private Request request = null;
public String requestMessage;
private Socket socket = null;
public Handler(Socket socket)
{
this.socket = socket;
this.headersFrame = new HttpHeadersFrame();
headersFrame.setVisible(true);
}
public void processRequest()
{
requestMessage = request.toString(System.getProperty("line.separator"));
headersFrame.getRequestTextArea().append(requestMessage);
}
}
当我运行代理时,我在 JTextArea 中没有收到任何消息!任何帮助将不胜感激
I have one class named handler and this class process the http request comming from the browser and i want to display the http headers of the request in JTextArea of another class named HttpHeadersFrame! This is what i have tried
public class Handler
{
HttpHeadersFrame headersFrame; //This frame contains JTextArea component
private Request request = null;
public String requestMessage;
private Socket socket = null;
public Handler(Socket socket)
{
this.socket = socket;
this.headersFrame = new HttpHeadersFrame();
headersFrame.setVisible(true);
}
public void processRequest()
{
requestMessage = request.toString(System.getProperty("line.separator"));
headersFrame.getRequestTextArea().append(requestMessage);
}
}
When i run the proxy i don't get any message in JTextArea! Any help would be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您的代码片段中没有正确使用 Swing 组件。
在使用之前,您应该在构造函数中初始化
requestTextArea
字段。而且您还必须使用语句将其添加到框架
您应该将
HttpHeadersFrame
实例传递给您的 Handler 类,使该实例可见,即并将
requestMessage
附加到该实例的 textArea 字段会起作用的。Firstly, there is lack of proper usage of Swing components in your snippet.
You should initialize the
requestTextArea
field in constructor before using it.And also you have to add it to frame with a statement
You should pass
HttpHeadersFrame
instance to your Handler class, make this instance visible, i.e.And appending
requestMessage
to this instance's textArea field will work.在
processRequest
方法中,您可以针对每个传入请求创建一个新的HttpHeadersFrame
。除非您显示所有这些框架,否则您将不断更新不可见的框架,而不是唯一的可见框架。因此,将可见的 HttpHeadersFrame 实例传递给您的 Handler 类 iso 创建新实例,您的问题将得到解决。哦,请确保更新事件调度线程上的 Swing 组件。
In your
processRequest
method you create a newHttpHeadersFrame
on each incoming request. Unless you display all those frames, you will constantly updating a non-visible frame instead of the one-and-only visible one. So pass a visibleHttpHeadersFrame
instance to yourHandler
class iso creating new instances and your problem will be solved.Oh, and please make sure you update the Swing components on the Event Dispatch Thread.