如何在另一个对象的 JTextArea 中显示一个对象的数据?

发布于 2025-01-06 02:26:27 字数 766 浏览 3 评论 0原文

我有一个名为 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 技术交流群。

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

发布评论

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

评论(2

风向决定发型 2025-01-13 02:26:27

首先,您的代码片段中没有正确使用 Swing 组件。
在使用之前,您应该在构造函数中初始化 requestTextArea 字段。
而且您还必须使用语句将其添加到框架

this.getContentPane().add(requestTextArea);

您应该将 HttpHeadersFrame 实例传递给您的 Handler 类,使该实例可见,即

headersFrame = new HttpHeadersFrame();
headersFrame.setVisible(); 

并将 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

this.getContentPane().add(requestTextArea);

You should pass HttpHeadersFrame instance to your Handler class, make this instance visible, i.e.

headersFrame = new HttpHeadersFrame();
headersFrame.setVisible(); 

And appending requestMessage to this instance's textArea field will work.

暗喜 2025-01-13 02:26:27

processRequest 方法中,您可以针对每个传入请求创建一个新的 HttpHeadersFrame。除非您显示所有这些框架,否则您将不断更新不可见的框架,而不是唯一的可见框架。因此,将可见的 HttpHeadersFrame 实例传递给您的 Handler 类 iso 创建新实例,您的问题将得到解决。

哦,请确保更新事件调度线程上的 Swing 组件。

In your processRequest method you create a new HttpHeadersFrame 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 visible HttpHeadersFrame instance to your Handler 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.

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