在JFrame中看不到进度栏

时间:2019-05-26 00:32:27

标签: progress-bar

我有一个类,该类的方法需要很长时间才能计算方程式的参数,因此在for循环迭代进行计算时,需要显示一个进度条。我正在尝试调用一个方法,该方法调用扩展JFrame的类,而该类又调用另一个具有扩展SwingWorker进度条的类,但是当我初始化jframe时,jframe上没有任何内容,并且为空白。为什么Jframe为空白。

我检查了它是否正在运行,并且循环和进度条循环都在运行。我还向Jframe类添加了JLabel,但它也没有出现。

        Public class regression{

        // method where i am initializing the jframe
         public void approximate(){

     this.findDerivative(this.exponential,this.quadratic);

     matrix expAve = new matrix(3,1);

     double beta1 = 0.9;

         loading frame = new loading();
         frame.setPreferredSize(new Dimension(500,500));
         frame.pack();
         frame.setVisible(true);

     for(int i=0;i<100000000;i++){

     matrix prev = expAve;

         expAve.times(beta1);

         matrix a = this.estimate.subtract(expAve);

     this.computeGradient(a);

     this.gradient.times(scale);

         prev.times(beta1);

         expAve = prev.addition(this.gradient);

     this.estimate = this.estimate.subtract(expAve);

         this.calcResidue();        
         }

             }

         //progress bar class

         import java.awt.Dimension;
         import java.util.List;

         import javax.swing.JProgressBar;
         import javax.swing.SwingWorker;

         public class progressBar extends SwingWorker<Void, Integer> {

     private final JProgressBar progress;

     public progressBar(JProgressBar progress) {

     this.progress = progress;
         this.progress.setVisible(true);        
     this.progress.setStringPainted(true);
         this.progress.setPreferredSize(new Dimension(500,100));

       }


    @Override
    protected Void doInBackground() throws Exception {

     for (int i = 100000000; i > 0; i--) {
         final int progr =((100 * (100000000 - i)) /100000000);
         publish(progr);
         setProgress(progr);

     }

     return null;

   }

 @Override
       protected void process(List<Integer> chunks) {
     progress.setValue(chunks.get(chunks.size() - 1));
     super.process(chunks);
      }
      @Override
     protected void done() {
     progress.setValue(100);
     }
     }

     import java.awt.BorderLayout;
     import java.awt.Color;
     import java.awt.Dimension;

     import javax.swing.JFrame;
     import javax.swing.JLabel;
     import javax.swing.JProgressBar;

    //extending jframe class 
    public class loading extends JFrame{
public loading() {
setLayout(new BorderLayout());
    JProgressBar progress = new JProgressBar();
    JLabel j = new JLabel("loading");
    add(j, BorderLayout.NORTH);
    add(progress, BorderLayout.SOUTH);
    progressBar worker = new progressBar(progress);
    worker.execute();
    j.setPreferredSize(new Dimension(500,50));

}
    }

 //main jframe where i call the jpanel vertex which calls the jframe loading
  import java.awt.Graphics;
   import java.awt.Graphics2D;
 import java.util.Random;
 import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.Font;

 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JProgressBar;

 class graph extends JFrame {

 public graph() {

    super("Scatterplot");

    double[][]p = new double[][]{{2,1},{3,10},{4,99},{3.6,34}};

    vertex v = new vertex(p,6, false, false, true);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    getContentPane().add(v);

    pack();

    setLocationRelativeTo(null);

    setVisible(true);



   }



   } 
   //jpanel where a paint method is creating a graph and where I want the 
  //loading Jframe to be initialized and show the loading progress.
   import java.awt.*;
   import javax.swing.JFrame;
   import javax.swing.JPanel;
   import javax.swing.JLabel;

   import java.awt.geom.Ellipse2D;
   import java.awt.geom.Line2D;
   import java.util.List;

   import javax.swing.JProgressBar; 
   import javax.swing.SwingWorker;
   import javax.swing.border.EmptyBorder;


   class vertex extends JPanel{

   //data points of all the transactions (date,amount)
   public double[][] points;

   public JLabel[] yInts = new JLabel[12];

   public JLabel[] ints;

   public double max = 0.0;

   public double axis = 0.0;

   public double size = 800.0;

   public double yAxis = size/10.0;

   public predictionAL lobf;

   public regression n;

   public boolean linear;


 public vertex(double[][] p, double months, boolean linear, boolean expo, 
 boolean quad){

 ints = new JLabel[(int) months];

 this.points = new double[p.length][p[0].length];

 this.linear = linear;

 for(int x=0;x<p.length; x++){

    for(int y=0;y<p[0].length;y++){

        points[x][y] = p[x][y];

    }

 } 


for(int x=0;x<p.length; x++){

        if(Double.compare(p[x][1], this.max)>0){

            this.max = p[x][1];

        }
    }

//max is the nearest tens
max = (10.0*Math.ceil(max/10.0));

axis = size/months;

setPreferredSize(new Dimension((int)(size)+20,(int)(size)+20));

setLayout(null);

if(this.linear){
lobf = new predictionAL(p); 

}else{

n = new regression(expo, quad, p);  


 }

 }



 public void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;

// (0,0) = (0,250) on frame.

//makes the points denser than the axis

//label the marks on the axis, where 10 is then 20 ... 

g2d.setStroke(new BasicStroke(1));

//graph lines
for(int x=0; x <7; x++){

    g2d.setColor(Color.red);
    //y-axis lines , x=
    g2d.draw(new Line2D.Double(x*axis,0,x*axis,size));

}

for(int x=0; x <11; x++){

    g2d.setColor(Color.red);
    //x-axis lines, y= 
    g2d.draw(new Line2D.Double(0, x*yAxis,size,x*yAxis));

}

g2d.setStroke(new BasicStroke(3));


g2d.setColor(Color.black);
for(int t=0; t <ints.length; t++){

    ints[t] = new JLabel();

    add(ints[t]);

    //x axis
    ints[t].setSize(200,13);

    ints[t].setLocation((int)(t*axis), (int)(size));

    if(t!=0){
        ints[t].setText(Integer.toString(ints.length-t));   

    }

    ints[t].setFont(new Font("Comic Sans MS", Font.PLAIN, 13));


}

for(int t=0; t <11; t++){
    yInts[t] = new JLabel();

    add(yInts[t]);

    yInts[t].setSize(200,13);

            yInts[t].setLocation(0,(int) (yAxis*(10-t)));   

            if(t!=0){

                yInts[t].setText(Integer.toString((int)(this.max/10.0)*t)); 
            }else{
                yInts[t].setText("(6,0)");  

            }


            yInts[t].setFont(new Font("Comic Sans MS", Font.PLAIN, 13));            

}
//plot points 
g2d.setStroke(new BasicStroke(5));

for(int x=0; x<points.length; x++){

    double z = (points[x][0]);

    double y = (points[x][1]);

    double ratio = y/max;

    g2d.draw(new Ellipse2D.Double(axis*(ints.length-z), (size)- 
   (size)*ratio,2,2));      
}

g2d.setColor(Color.BLACK);

g2d.setStroke(new BasicStroke(2));

//time axis, x- axis
g2d.draw(new Line2D.Double(0,(size),size,(size)));

//amount axis, y -axis
g2d.draw(new Line2D.Double(0,(size),0,0));

if(this.linear){

    double yRatio = (-ints.length*lobf.slope+lobf.yInt);


 //draw line of best fit
  g2d.draw(new Line2D.Double(0,size-(yRatio*size/max),size - axis* 
  (lobf.yInt/lobf.slope),size));    

  }else{

    n.approximate();

    System.out.println(n.estimate.arrayM[0][0]);

    int[] xPoints = {5, 10 , 15, 20, 25, 30 ,35 }; 

    int[] yPoints = new int[xPoints.length];

    if(n.exponential){

        for(int i=0; i<xPoints.length;i++){

            yPoints[i] = (int) ((int)this.n.estimate.arrayM[0] 
        [0]*Math.exp(this.n.estimate.arrayM[1] 
      [0]*xPoints[i])+n.estimate.arrayM[2][0]);     

        }


    }else{


        for(int i=0; i<xPoints.length;i++){

            yPoints[i] = (int) ((int)this.n.estimate.arrayM[0] 
        [0]*xPoints[i]*xPoints[i] + this.n.estimate.arrayM[1][0]*xPoints[i] 
     + this.n.estimate.arrayM[2][0]);

        }


    }

    g2d.drawPolyline(xPoints, yPoints, 3);

    }



   }





  }

简而言之,jframe上没有进度条,并且在jframe类中添加了jlabel。请帮助。

0 个答案:

没有答案