ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 在计算时在JTextArea中显示文本

2019-09-29 14:01:10  阅读:297  来源: 互联网

标签:jtextarea java concurrency background swing


我正在编写的应用程序包括JButton和JTextArea.单击该按钮会导致计算时间过长,从而导致JTextArea中显示文本.即使计算时间很长,我也可以随时随地获得中间结果(例如,想想一个近似于100位数的pi的应用程序 – 每隔几秒我就可以编写另一个数字).问题是,即使我写(在ActionListener类中,因为按钮调用了计算)来将JTextArea的文本设置为某些东西,它在计算完成时也不显示,我只能看到结束结果,计算结束后.

为什么会这样,我该如何解决?

先感谢您.

解决方法:

你的问题是你在主Swing线程EDT中进行了长时间的计算,这将冻结整个GUI,直到进程完成为止.一种解决方案是使用后台线程进行计算,并使用一种简单的方法来使用SwingWorker创建主Swing线程(EDT)的线程背景,并将中间结果发布/处理到JTextArea中.有关SwingWorkers和EDT的更多信息,请查看此处:Concurrency in Swing

另外,如果你提供了一个不错的sscce,我们可能会给你一个更详细的回复,甚至可能是样本代码.

SSCCE的一个例子:

import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.List;
import javax.swing.*;

public class InterimCalc {
   private JPanel mainPanel = new JPanel();
   private JTextField resultField = new JTextField(10);
   private JButton doItBtn = new JButton("Do It!");
   private DecimalFormat dblFormat = new DecimalFormat("0.0000000000");
   private SwingWorker<Void, Double> mySwingWorker = null;

   public InterimCalc() {
      mainPanel.add(doItBtn);
      mainPanel.add(resultField);
      displayResult(0.0);

      doItBtn.addActionListener(new DoItListener());
   }

   public void displayResult(double result) {
      resultField.setText(dblFormat.format(result));
   }

   public JPanel getMainPanel() {
      return mainPanel;
   }

   private class DoItListener implements ActionListener {

      public void actionPerformed(ActionEvent e) {
         if (mySwingWorker != null && !mySwingWorker.isDone()) {
            mySwingWorker.cancel(true);
         }
         displayResult(0.0);
         mySwingWorker = new MySwingWorker();
         mySwingWorker.execute();
      }
   }

   private class MySwingWorker extends SwingWorker<Void, Double> {

      private static final int INTERIM_LENGTH = 10000; // how many loops to do before displaying

      @Override
      protected Void doInBackground() throws Exception {
         boolean keepGoing = true;
         long index = 1L;
         double value = 0.0;
         while (keepGoing) {
            for (int i = 0; i < INTERIM_LENGTH; i++) {
               int multiplier = (index % 2 == 0) ? -1 : 1;
               value += (double)multiplier / (index);
               index++;
            }
            publish(value);
         }
         return null;
      }

      @Override
      protected void process(List<Double> chunks) {
         for (Double dbl : chunks) {
            displayResult(dbl);
         }
      }

   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("Decay Const");
      frame.getContentPane().add(new InterimCalc().getMainPanel());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

标签:jtextarea,java,concurrency,background,swing
来源: https://codeday.me/bug/20190929/1831651.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有