在显示文字JTextArea中计算时 [英] Showing text in JTextArea while calculating

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

问题描述

我写的应用程序包括,除其他外,一个JButton和JTextArea中。按钮的点击引发长时间的计算,从而在JTextArea中显示的文本。即使计算是漫长的,我可以在旅途中等成绩(想想,例如,近似于圆周率高达100位的应用程序 - 每隔几秒钟,我可以写另一个数字)。问题是,即使我写的(在ActionListener的类是因为该按钮调用的计算)设置JTextArea中的东西的文字,它不同时计算完成后显示的,我只能看到最后结果,经过计算就结束了。

An application I am writing consists, among others, a JButton and a JTextArea. A click on the button leads to a long calculation, resulting in a text shown in the JTextArea. Even though the calculation is long, I can have middle-results on the go (think, for example, of an application which approximates pi up to 100 digits - every few seconds I could write another digit). The problem is, that even if I write (being in the ActionListener class because the button invoked the calculation) to set the text of the JTextArea to something, it isn't shown while the calculation is done, and I can only see the end result, after the calculation is over.

为什么会这样,我怎么能解决这个问题?

Why is it so, and how can I fix it?

感谢您提前。

推荐答案

您的问题是,你正在做的主要的Swing线程,EDT长计算,直到进程完成自己这将会冻结您的整个GUI 。一个解决办法是使用你的计算后台线程,和一个简单的方法来做到这一点使用的SwingWorker创建一个线程后台主Swing线程,在EDT和发布/进程的中期业绩进入的JTextArea。欲了解更多关于SwingWorkers和EDT,请看看这里:并发在Swing

Your problem is that you're doing a long calculation in the main Swing thread, the EDT, and this will freeze your entire GUI until the process has completed itself. A solution is to use a background thread for your calculation, and an easy way to do this it to use a SwingWorker to create a thread background to the main Swing thread, the EDT, and publish/process the interim results into the JTextArea. For more on SwingWorkers and the EDT, please look here: Concurrency in Swing

另外,如果你提供一个体面的 SSCCE 我们大概可以给你甚至样品$ C $一个更详细的回应角

Also, if you provide a decent sscce we can probably give you a more detailed response perhaps even with sample code.

一个例子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中计算时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆