我有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显示为一个空窗口,并且在ClassA的所有进程/计算完成之前不会显示任何内容。然后它显示其内容,但最后更新来自ClassA的数据。 任何想法或解决方案?
答案 0 :(得分:3)
您没有说明如何调用进行各种计算的ClassA 方法。我只能猜测你在一个事件调度线程中调用它们(有关详细信息,请参阅The Event Dispatch Thread),并且在ClassA完成其处理之前,Java无法呈现任何内容。
如果是这种情况,您应该在其他线程中运行这些计算。最方便的方法是using SwingWorker
。