Can a progress bar be used in a class outside main?

后端 未结 3 825
孤独总比滥情好
孤独总比滥情好 2020-11-21 06:47

Right now, my main just calls a gui with 10 rows. Based on how many of those rows have text, 1 of 9 classes is called (two rows must have text). The called class performs

3条回答
  •  南旧
    南旧 (楼主)
    2020-11-21 07:20

    Thanks for the help. I started with trying to use the first response, but I couldn't get the bar to run concurrently, and it ran when the program finished. I'm sure it would work, but I wasn't able to figure it out. Using trashgod's response and some other examples, I was able to get it to work using SwingWorker. Unfortunately, I don't totally understand how it works, but I'll take it for now.

    The gui and method to run the calculations are called in another class first:

    iterations = (int) (MPay - (M1Start + M2Start));
            twoLoan myLoan = new twoLoan();
            myLoan.createGui(iterations);
            myLoan.runCalcs(MPay, Step, L1, L2, C1, C2, IM1, IM2, M1Start, M2Start);
    

    Then it runs as follows:

    public class twoLoan extends JFrame {
    
        JFrame progressFrame;
        JProgressBar progressBar;
        JLabel label = new JLabel("Calculating...");;
        Container pane;
    
        double amountSaved = 0;
        int i = 0;
        int iterations;
    
        public void createGui(int iterations) {
               //Create all components
              progressFrame = new JFrame("Calculation Progress");
              progressFrame.setSize(300, 100);
              pane = progressFrame.getContentPane();
              pane.setLayout(null);
              label = new JLabel("Calculating...");
              label.setBounds(115, 35, 200, 25);
              progressBar = new JProgressBar(0, iterations);
              progressBar.setBounds(10, 10, 280, 20);
              progressBar.setStringPainted(true);
              //Add components to pane
              pane.add(progressBar);
              pane.add(label);
              //Make frame visible
              progressFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              progressFrame.setResizable(false); //No resize
              progressFrame.setLocationRelativeTo(null);
              progressFrame.setVisible(true);
        }
    
        public void runCalcs (double MP, double StepAmt, double L1, double L2,
                double C1, double C2, double IM1, double IM2, double M1Start, double M2Start) {
    
            progressBar.setIndeterminate(false);
            TwoWorker task = new TwoWorker(MP, StepAmt, L1, L2, C1, C2, IM1, IM2, M1Start, M2Start);
            task.addPropertyChangeListener(new PropertyChangeListener() {
    
                @Override
                public void propertyChange(PropertyChangeEvent e) {
                    if ("progress".equals(e.getPropertyName())) {
                        progressBar.setIndeterminate(false);
                        progressBar.setValue((Integer) e.getNewValue());
                    }
                }
            });
            task.execute();
        } //end method runCalcs
    
        public class TwoWorker extends SwingWorker {
    
            private final double MP, StepAmt,L1, L2,
                C1, C2, IM1, IM2, M1Start, M2Start;
    
            public TwoWorker(double MPa, double StepAmta, double L1a, double L2a,
                double C1a, double C2a, double IM1a, double IM2a, double M1Starta, double M2Starta) {
    
                MP = MPa;
                StepAmt = StepAmta;
                L1 = L1a;
                L2 = L2a;
                C1 = C1a;
                C2 = C2a;
                IM1 = IM1a;
                IM2 = IM2a;
                M1Start = M1Starta;
                M2Start = M2Starta;
               }
            @Override
            protected Double doInBackground() {
    
                double M1 = M1Start;
                double M2 = M2Start;
    
            // Set MinLoop as maximum to start
            // Loan 1
            double N1 = (Math.log10(1 - IM1 * L1 / M1) * -1)/Math.log10(1 + IM1);
        double M1Sum = M1 * N1;
        // Loan 2
        double N2 = (Math.log10(1 - IM2 * L2 / M2) * -1)/Math.log10(1 + IM2);
        double M2Sum = M2 * N2;
        double minLoop = M1Sum + M2Sum;
        double MTotal = 0;
    
            // Define variables for mins
        double MP1 = 0;
        double MP2 = 0;
        double NP1 = 0;
        double NP2 = 0;
        double MP1Sum = 0;
        double MP2Sum = 0;
    
            while ( M1 <= MP - M2Start && M2 >= M2Start ) {
                N1 = (Math.log10(1 - IM1 * L1 / M1) * -1)/Math.log10(1 + IM1);
                M1Sum = N1 * M1;
                N2 = (Math.log10(1 - IM2 * L2 / M2) * -1)/Math.log10(1 + IM2);
                M2Sum = N2 * M2;
                MTotal = M1Sum + M2Sum;
                if (MTotal < minLoop) {
                    minLoop = MTotal;
                    MP1 = M1;
                    MP2 = M2;
                    NP1 = N1;
                    NP2 = N2;
                    MP1Sum = M1Sum;
                    MP2Sum = M2Sum;
                } // end if
                            i++;
                            progressBar.setValue(i);
                        M1 = M1 + StepAmt;
                M2 = MP - M1;
                // Reset monthly sums
                M1Sum = 0;
                M2Sum = 0;
            } // end while
    
            System.out.printf("MP1 = %.2f\n", MP1);
            System.out.printf("MP2 = %.2f\n", MP2);
            System.out.printf("NP1 = %.2f\n", NP1);
            System.out.printf("NP2 = %.2f\n", NP2);
            System.out.printf("MP1Sum = %.2f\n", MP1Sum);
            System.out.printf("MP2Sum = %.2f\n", MP2Sum);
                    System.out.printf("MTotal = %.2f\n", minLoop);
                    System.out.printf("i = %d\n",i);
                    System.out.printf("M1Start = %.2f\n", M1Start);
            System.out.printf("M2Start = %.2f\n", M2Start);
                    System.out.printf("MP= %.2f\n",MP);
    
        // if there's a value for current payments, calculate amount saved
        if( C1 > 0 ) {
            double CN1 = (Math.log10(1 - IM1 * L1 / C1) * -1)/Math.log10(1 + IM1);
            double CT1 = CN1 * C1;
    
            double CN2 = (Math.log10(1 - IM2 * L2 / C2) * -1)/Math.log10(1 + IM2);
            double CT2 = CN2 * C2;
    
            double CTotal = CT1 + CT2;
            amountSaved = CTotal - minLoop;
            } // end if
    
            return null;
    
        } // end doInBackGround
    
            @Override
            protected void done() {
                label.setBounds(133, 35, 200, 25);
                label.setText("Done!");
            }
        } // end TwoWorker
    
    
        public double savedReturn() {
            return amountSaved;
        }
    
    } // end class twoLoans
    

提交回复
热议问题