在进行计算时显示进度条 [英] Displaying a Progress Bar while a calculation is occuring

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

问题描述

我正在编写代码以计算Pi的值,有时可能需要很长时间才能计算出.我添加了一个进度条以显示进度,但是代码完全按照我的指示执行,它在计算后打开进度条,然后立即将其关闭(当值达到100时,它将关闭). 我试图将进度条的代码粘贴到循环中,但是不久我意识到解决了该问题的方法,但是创建了多个进度条窗口. 如果放在计算之前,进度条将保持为0(很明显) 我在下面显示了我的代码:

I am writing a code in order to calculating the value of Pi, and sometimes could take a long time to calculate. I added a progress bar to show progress, but the code does exactly what I told it to, it opens the progress bar after the calculations and then immediately closes it (It closes when the value reaches 100.) I have attempted to stick the code for the progress bar into the loop, but soon I realized that solves the solution, but creates multiply progress bar windows. If placed before calculations, the progress bar remains at 0 (Obviously) I have displayed my code below:

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.border.Border;
import java.util.Scanner;

public class PicCalc 
{
public static void main(String args[]) 
{
  Scanner keyboard = new Scanner(System.in);
  double i = 0;
    double stage = 1;
  int iterations = 1;
    int x = 3; //Simply a variable to calculate pi
  double y = -1.0; // Another variable for pi
    double myPi; //This is my calculation of Pi, don't eat it. 

  System.out.println("Hello! I calculate Pi and compare it to the acutal value of Pi!");
  System.out.println("How many iterations?");
  iterations = keyboard.nextInt();

  //Calculates Pi
  for (i = 1.0; i <= iterations; i++)
  {
         stage = stage + y/x;
         y = - y; //Flips Sign
         x+=2; 
  }

    myPi = 4*stage;
    System.out.println("My Pi: " + myPi);
    //STOP CALCULATING PI

    //CALCULATE PERCENT COMPLETE
  double percent = 100*(i/iterations);
  int intPercent = (int) (percent + 0.5); //Adds .5 in order to round.
  //STOP CALCULATING PERCENT COMPLETE

  //MAKES LOADING SCREEN
  JFrame f = new JFrame("Loading...");
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Container content = f.getContentPane();
  JProgressBar progressBar = new JProgressBar();
  progressBar.setValue(intPercent);
  progressBar.setStringPainted(true);
  Border border = BorderFactory.createTitledBorder("Calcualating...");
  progressBar.setBorder(border);
  content.add(progressBar, BorderLayout.NORTH);
  f.setSize(300, 100);
  f.setVisible(true);
  //END OF MAKING LOADING SCREEN

 //CLOSES LOADING SCREEN WHEN 100% IS ACHIEVED
  if (percent >= 100)
  {
     f.dispose();
  }      
  //END OF CLOSING SCREEN HERE

推荐答案

编辑:我认为,如果您从我的回答中考虑到逻辑更改,并将其作为 John 建议,您将一切准备就绪;)

EDIT: I think if you take the changes in logic into account from my answer, and place it in a background process as John suggests, you'll be all set ;)

我认为您可能希望将完成百分比的计算移到for循环中,以便在进行迭代时更新进度.

I think you may want to move your calculation of the percentage complete into your for loop so that it will update the progress as you make your iterations.

//MAKES LOADING SCREEN
JFrame f = new JFrame("Loading...");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = f.getContentPane();
JProgressBar progressBar = new JProgressBar();
progressBar.setStringPainted(true);
Border border = BorderFactory.createTitledBorder("Calcualating...");
progressBar.setBorder(border);
content.add(progressBar, BorderLayout.NORTH);
f.setSize(300, 100);
f.setVisible(true);

double percent = 0;
int intPercent = 0;
progressBar.setValue(intPercent);
//Calculates Pi
for (i = 1.0; i <= iterations; i++)
{
    stage = stage + y/x;
    y = - y; //Flips Sign
    x+=2; 
    //CALCULATE PERCENT COMPLETE
    percent = 100*(i/iterations);
    intPercent = (int) (percent + 0.5); //Adds .5 in order to round.
    progressBar.setValue(intPercent);
    //STOP CALCULATING PERCENT COMPLETE
}

myPi = 4*stage;
System.out.println("My Pi: " + myPi);
//STOP CALCULATING PI

这篇关于在进行计算时显示进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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