进度条可以在主程序之外的类中使用吗?现在,我的主调用一个10行的gui。根据这些行中有多少行有文本,将调用9个类中的一个(两行必须有文本)。被调用的类执行我希望将进度条绑定到的计算。下面是一个被调用类的示例(每个类相似,但不同到足以保证一个新类)。我认为这个问题违反了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;
3 回答
叮当猫咪
TA贡献1776条经验 获得超12个赞
JFrame
Runnable
Runnable
SwingUtilities.invokeLater
将GUI位的创建移动到一个方法中:
public void createComponents () { SwingUtilities.invokeLater(new Runnable() { public void run() { //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); } }); }
然后,我将对calc中的两个GUI操作进行处理:
private void updateProgressBar(final int i) { SwingUtilities.invokeLater(new Runnable() { public void run() { progressBar.setValue(i); //no need for the following //progressBar.repaint(); } }); } private void killDialog() { SwingUtilities.invokeLater(new Runnable() { public void run() { progressFrame.setVisible(false); } }); }
添加回答
举报
0/150
提交
取消