Java-进度栏可以在main以外的课程中使用吗?

现在,我的主要人员只调用了10行的gui。根据这些行中有多少行有文本,将调用9类中的1个(两行必须有文本)。被调用的类执行我希望绑定进度条的计算。这是其中一个被调用类的示例(每个类都是相似的,但又有足够的区别,以至于需要一个新的类。)我认为问题是违反了EDT规则,但是我在它们上看到的所有示例都涉及一个主要类。论点。运行代码时会出现该框架,但是直到完成所有计算后进度条才会更新。

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

回答:

SwingWorker为此非常理想。下面的示例在后台执行一个简单的迭代,同时在一个窗口中报告进度和中间结果。你可以在合适的SwingWorker构造函数中传递所需的任何参数。

import java.awt.EventQueue;

import java.awt.GridLayout;

import java.beans.PropertyChangeEvent;

import java.beans.PropertyChangeListener;

import java.text.DecimalFormat;

import java.util.List;

import javax.swing.*;

/** @see http://stackoverflow.com/questions/4637215 */

public class TwoRoot extends JFrame {

private static final String s = "0.000000000000000";

private JProgressBar progressBar = new JProgressBar(0, 100);

private JLabel label = new JLabel(s, JLabel.CENTER);

public TwoRoot() {

this.setLayout(new GridLayout(0, 1));

this.setTitle("√2");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.add(progressBar);

this.add(label);

this.setSize(161, 100);

this.setLocationRelativeTo(null);

this.setVisible(true);

}

public void runCalc() {

progressBar.setIndeterminate(true);

TwoWorker task = new TwoWorker();

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();

}

private class TwoWorker extends SwingWorker<Double, Double> {

private static final int N = 5;

private final DecimalFormat df = new DecimalFormat(s);

double x = 1;

@Override

protected Double doInBackground() throws Exception {

for (int i = 1; i <= N; i++) {

x = x - (((x * x - 2) / (2 * x)));

setProgress(i * (100 / N));

publish(Double.valueOf(x));

Thread.sleep(1000); // simulate latency

}

return Double.valueOf(x);

}

@Override

protected void process(List<Double> chunks) {

for (double d : chunks) {

label.setText(df.format(d));

}

}

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

TwoRoot t = new TwoRoot();

t.runCalc();

}

});

}

}

以上是 Java-进度栏可以在main以外的课程中使用吗? 的全部内容, 来源链接: utcz.com/qa/402840.html

回到顶部