現在,我的主要只是調用10行gui。根據有多少行具有文本,將調用9個類中的1個(兩行必須包含文本)。被調用的類執行計算,我想要將進度條綁定到。下面是一個被調用的類的例子(每個類都是類似的,但是不同的是需要一個新的類)。我相信這個問題違反了美國東部時間的規則,但是我看到的所有例子都涉及到一個main論據。框架在代碼運行時出現,但進度條在所有計算完成前都不會更新。一個進度條可以在主要的課外使用嗎?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class twoLoan extends JFrame {
static JFrame progressFrame;
static JProgressBar progressBar;
static Container pane;
double amountSaved = 0;
int i = 0;
public void runCalcs(Double MP, Double StepAmt,
Double L1, Double L2, Double C1, Double C2,
Double IM1, Double IM2, Double M1Start, Double M2Start) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
int iterations = (int) (MP - (M1Start * M2Start));
//Create all components
progressFrame = new JFrame("Calculation Progress");
progressFrame.setSize(300, 100);
pane = progressFrame.getContentPane();
pane.setLayout(null);
progressFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
progressBar = new JProgressBar(0, iterations);
//Add components to pane
pane.add(progressBar);
//Position controls (X, Y, width, height)
progressBar.setBounds(10, 10, 280, 20);
//Make frame visible
progressFrame.setResizable(false); //No resize
progressFrame.setVisible(true);
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
M1 = M1 + StepAmt;
M2 = MP - M1;
// Reset monthly sums
M1Sum = 0;
M2Sum = 0;
i++;
progressBar.setValue(i);
progressBar.repaint();
if (i >= iterations) {
progressFrame.dispose();
}
} // end while
// 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 method runCalcs
//Workbook wb = new HSSFWorkbook();
public double savedReturn() {
return amountSaved;
}
} // end class twoLoans
順便說一下,`main()`只是一個將`Runnable`傳遞給事件派發線程的方法。然後`Runnable`構造GUI並在`TwoWorker`的一個實例上調用`execute()`。 – trashgod 2011-01-09 19:49:16