【问题标题】:How to display a text in textArea?如何在 textArea 中显示文本?
【发布时间】:2018-07-15 19:02:33
【问题描述】:

我试图在 GUI 中的 textArea 中显示 X.509 证书的公钥,而不是使用 System.out.println()。当我尝试使用setText() 在textArea 中显示它时,它没有在textArea 中显示它。以下方法readCertificate提取证书的字段,ActionPerformed(ActionEvent e)只是一个激活动作的底部:

public void actionPerformed(ActionEvent e){
    File f = new File("/Users/AhmadAseeri/Desktop/cer.cer");
    Read r = new Read();
    r.readCertificate(f);
}

public void readCertificate(File f) throws Exception {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
    while (in.available() > 0) {
        Certificate cert = cf.generateCertificate(in);
        String c=String.valueOf(cert.getPublicKey());
        JTextArea textArea= new JTextArea();
        textArea.setText(c);  
    }
    in.close();
} 

【问题讨论】:

  • 发布编译器异常消息或堆栈跟踪。
  • 您是遇到运行时错误还是编译器错误?
  • 尝试添加文本,或者尝试在 while 中连接一个字符串并在循环外设置文本。

标签: java swing jtextarea


【解决方案1】:

您正在使用 JTextArea 但您没有将它添加到任何容器中。

那样,它永远不会显示。

Here 您可以找到一个使用该组件构建简单 GUI 的示例。

【讨论】:

    【解决方案2】:

    如前所述:

    • 您不会将文本区域添加到任何容器中,因此它永远不可见。
    • 您在while 循环中创建了这个文本区域,这是错误的。您必须在 GUI 初始化时创建文本区域并将其添加到容器中。
    • 您可能想尝试append(String str) 方法而不是setText(String str)

    另外

    输入-输出操作是耗时的任务,可能会阻塞 Event Dispatch Thread(又名 EDT),导致 GUI 无响应。为避免此问题,请考虑使用SwingWorker 在后台线程中执行 IO 操作并更新 EDT 中的 Swing 组件。在Concurrency in Swing trail 中查看有关此问题的更多信息

    示例

    public void readCertificate(final File f) {        
        SwingWorker<Void, String> worker = new SwingWorker<Void, String>() {
            @Override
            protected Void doInBackground() throws Exception {
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
                BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
                while (in.available() > 0) {
                    Certificate cert = cf.generateCertificate(in);
                    String c = String.valueOf(cert.getPublicKey());
                    publish(c);
                }
                in.close();
                return null;
            }
    
            @Override
            protected void process(List<String> chunks) {
                for(String key : chunks) {
                    textArea.append(key + System.lineSeparator());
                }
            }            
        };
        worker.execute();
    }
    

    【讨论】:

      【解决方案3】:

      您的问题是您在 while 循环内创建 textArea,在方法 readCertificate(File f) 内:

      public void readCertificate(File f) throws Exception {
          CertificateFactory cf = CertificateFactory.getInstance("X.509");
          BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
          while (in.available() > 0) {
              Certificate cert = cf.generateCertificate(in);
              String c=String.valueOf(cert.getPublicKey());
              JTextArea textArea= new JTextArea();//????????????????????????????????
              textArea.setText(c);  
          }
          in.close();
      } 
      

      您必须在框架/面板上或在需要时创建它,但不是在那里。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-29
        • 2019-10-19
        • 2020-08-26
        • 2014-10-20
        相关资源
        最近更新 更多