本文介绍了JTextArea不显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我在textarea中显示文本的功能中,我编写了以下代码行,但它没有显示任何文本

In my function for displaying text in textarea,i have written following lines of code but it is not displaying any text

        jTextArea1.setText( Packet +"\n" +jTextArea1.getText());

我使用swingworker执行后台任务,这是我的代码

I am using swingworker for performing background task,here is my code

public class SaveTraffic extends SwingWorker<Void, Void> {


public GUI f = new GUI();

@Override
public Void doInBackground() throws IOException {
              //some code
              sendPacket(captor.getPacket().toString());
              return null;
             }//end main function

@Override
public void done() {
    System.out.println("I am DONE");

}


public void sendPacket(String Packet) {

 f.showPackets(Packet);
}

}

以及我在GUI表单中编写的以下代码行

and the following lines of code i have written in my GUI form

 public  void showPackets(String Packet) {

 jTextArea1.append( Packet);

}

解决方案:
公共类SaveTraffic扩展SwingWorker {

Solution: public class SaveTraffic extends SwingWorker {

     public GUI f = new GUI();

     @Override
    public Void doInBackground() throws IOException {
    f.add(jTextPane1);
   // some code

   publish(captor.getPacket().toString());

   // the method below is calling sendPacket on the background thread
   // which then calls showPackets on the background thread
   // which then appends text into the JTextArea on the background thread
  //sendPacket(captor.getPacket().toString());

    return null;
   }

  @Override
   protected void process(List<String> chunks) {
   for (String text : chunks) {
     jTextPane1.setText(text);
     f.showPackets(text);
   }
  }

  @Override
  public void done() {
   System.out.println("I am DONE");

   }

}

推荐答案

你的问题是一个非常不完整的问题,一个没有足够的信息来允许答案,还有一个迫使我们猜测的问题,但基于你原来帖子中的这一行:

Yours is a very incomplete question, one without enough information to allow an answer, and one that forces us to guess, but based on this line in your original post:

我猜,我会打赌,你有一个Swing线程问题。您可能希望阅读并使用SwingWorker。

I'll guess, and I will bet money that you've got a Swing threading issue. You will probably want to read up on and use a SwingWorker.

从这里开始了解EDT和SwingWorkers:。

Start here to learn about the EDT and SwingWorkers: Concurrency in Swing.

是的,你的是一个Swing您在后台线程中进行Swing调用导致的并发问题。为避免这样做,您需要从doInBackground导出数据并在Swing事件线程上调用它。一种方法是通过发布/处理方法对:

Yes, yours is a Swing concurrency issue caused by your making Swing calls from within the background thread. To avoid doing this, you need to export the data from doInBackground and call it on the Swing event thread. One way to to do this is via the publish/process method pair:

public class SaveTraffic extends SwingWorker<Void, String> {

  public GUI f = new GUI();

  @Override
  public Void doInBackground() throws IOException {

     // some code

     publish(captor.getPacket().toString());

     // the method below is calling sendPacket on the background thread
     // which then calls showPackets on the background thread
     // which then appends text into the JTextArea on the background thread
     //sendPacket(captor.getPacket().toString());

     return null;
  }

  @Override
  protected void process(List<String> packetTextList) {
     for (String packetText : packetTextList) {
        sendPacket(packetText); //edit, changed to match your code
     }
  }

  @Override
  public void done() {
     System.out.println("I am DONE");

  }

  public void sendPacket(String Packet) {

     f.showPackets(Packet);
  }
}

检查我上面链接的教程和SwingWorker API有关详细信息。

Check the tutorial I linked to above and the SwingWorker API for more details on this.

这篇关于JTextArea不显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:50