簡體   English   中英

如何在運行確定要在該JTextArea中顯示的代碼的同時在JTextArea中完全顯示進度?

[英]How can I make a progress complete show in a JTextArea while running code that's figuring out what to display in that JTextArea?

我不知道這是否可能。 我正在制作彩票應用程序,並且嘗試使用盡可能少的GUI組件。 所以我有一個JTextArea應該顯示以下消息(例如):

“正在計算... 55.4%”

當我將其打印到控制台時,它顯示的很好,但不會將其打印到JTextArea。 我嘗試使用SwingUtilities.invokeLater,但這也不起作用。

    for (int x = 0; x < daysBetween; x++)
    {
      completion = "Calculating..." + df.format((100 * (x + 1)) / daysBetween) + "%";
      if (!textArea.getText().equals(completion))
      {
        textArea.setText(completion);
      }
      /*
      Here I have a lot of irrelevant code that compares your tickets to the winning tickets and counts your winnings and other statistics...
      */
    }

    ticketReport += "Computation time: " + getElapsedTime(start, System.nanoTime());
    ticketReport += "\nEnd date: " + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.YEAR); 
    ticketReport += "\nTotal # of tickets purchased: " + numOfTicketsPurchased;
    /*
    etc. with filling out the ticket report
    */
    textArea.setText(ticketReport);

可以猜到,我希望在上面的for循環中設置textArea的文本時更新JTextArea。 直到方法結束時它才更新JTextArea,這是我設置文本區域以顯示票證報告時的最底部。

彩票發燒申請

我的最終目標:我最終希望將其轉換為Android手機應用程序,因此這就是為什么我不想使用任何彈出式窗口或任何東西的原因。

/ *編輯:這暫時解決了問題,但是我遇到了另一個問題。 現在,當我嘗試通過使用參數true調用其cancel方法來取消SwingWorker時,它不會取消。 請參閱我的其他答案以了解解決該問題的方法。 * /

我想到了。 我將上述方法放入SwingWorker中,如下所示:

      SwingWorker<Void, Void> sw = new SwingWorker<Void, Void>()
      {
        @Override
        protected Void doInBackground() throws Exception
        {
          //method here
        }
      }

在該方法內部,我使用參數“(100 *(x + 1))/ daysBetween”調用setProgress方法以顯示正確的%完成率。 然后,在此之后,我添加了以下內容:

      sw.execute();
      sw.addPropertyChangeListener(new PropertyChangeListener()
      {
        @Override
        public void propertyChange(PropertyChangeEvent arg0)
        {
          textArea.setText("Calculating..." + sw.getProgress() + "%");
          if (sw.getProgress() == 100)
            textArea.setText(ticketReport);
        }
      });

它顯示的是整數百分比,而不是我最初想要的#。##,但是如果願意,我可以很容易地進行更改。

我最初的SwingWorker方法使其在JTextArea中顯示完成百分比狀態。 我添加了一個取消操作的選項,當我試圖取消操作時,“線程”(SwingWorker)不會取消。 當我取消並重新啟動時,它將不斷堆疊。 每次相繼開始而另一個相繼開始將導致計算的速度越來越慢,然后給出錯誤的信息。

我第二次這樣做的方式解決了原始問題和新問題。 我創建了一個新的私有類來擴展Thread,覆蓋其run()方法,然后在該方法的線程中插入要執行的代碼。 我注意到由於潛在的問題,不建議使用任何停止線程的方法,因此我創建了一個布爾值,以在用戶請求停止線程時進行設置,並在線程內定期讀取布爾值,然后在等於時退出真實。

我啟動線程的方法是重新初始化對象BackgroundCalc並調用其start()方法。

  private class BackgroundCalc extends Thread
  {
    public void run()
    {
      /*
      initializing stuff here
      */
      for (int x = 0; x < daysBetween; x++)
      {
        progress = (100 * (x + 1)) / daysBetween;
        if (destroyCalcThread) return;
        /*
        background calculations here
        */
      }
      /*
      finish up thread here
      */
    }
  }

我對線程沒有一點經驗,所以希望我在這里的努力以及我對它的解釋方式將幫助對這個概念沒有經驗的其他人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM