JFrame 弹出窗口不显示内容

JFrame popup not showing content

我有 ClassA 做一些处理,我希望它创建一个弹出式 Jframe ClassB,显示 ClassA 计算的一些统计数据。所以我做了这样的事情:

public class MainMenu extends javax.swing.JFrame {

public MainMenu() {
    initComponents();
}

private void initComponents() {

    calculateButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    calculateButton.setText("Calculate");
    calculateButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            calculateButtonActionPerformed(evt);
        }
    });

    pack();
}             

private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
    new ClassA().calculate();
}
public static void main(String args[]) {

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new MainMenu().setVisible(true);
        }
    });
}




public class ClassA {
private int i;
private ClassB classB;
public ClassA(){
    i = 3000;
    classB = new ClassB();
    classB.setVisible(true);


}

public void calculate(){
    int percentComplete;
    for (int j = 0 ; j<i ; j++){
        for (int x = 0; x<100000 ; x++)
            for (int y = 0; y<100000 ; y++)
                for (int z = 0; z<100000 ; z++);
        percentComplete = (int) (100 * (float)j/ (float) i);
        System.out.println(" "+percentComplete);
        classB.setProgress(percentComplete);
    }
}   

}

public class ClassB extends javax.swing.JFrame {

public ClassB() {
    initComponents();
}

private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jProgressBar1 = new javax.swing.JProgressBar();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jProgressBar1.setStringPainted(true);
    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
            .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(39, 39, 39)
            .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(52, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(181, Short.MAX_VALUE))
    );

    pack();
}

public void setProgress(int x){
    jProgressBar1.setValue ( x );
}


private javax.swing.JPanel jPanel1;
private javax.swing.JProgressBar jProgressBar1;

}

我面临的问题是 ClassB 的 JFrame 显示为空 window 并且在 ClassA 的所有 processes/calculations 完成之前不显示任何内容。然后它显示其内容,但包含来自 ClassA 数据的最新更新。 有什么想法或解决方案吗?

您没有说明如何调用那些进行各种计算的 A 类方法。我只能猜测您在事件分派线程中调用了所有这些(有关详细信息,请参阅 The Event Dispatch Thread)并且 Java 在 ClassA 完成其处理之前无法呈现任何内容。

如果是这种情况,您应该 运行 在其他线程中进行这些计算。最方便的方法是 using SwingWorker.