java swing进度条没有显示

时间:2014-04-23 12:34:49

标签: java swing progress-bar

当用户点击我的swing应用中的某些按钮时,我正在尝试显示不确定的进度条。我已经为进度条编写了一个通用类,每次点击这些按钮时都会调用它。进度条窗口显示在任务栏中,但不显示在屏幕上。为什么会发生这种情况,我该如何解决?

感谢您的帮助。

修改

SSCE for my application:Progress Bar class

package mainform;

public class ProgressBarIndeterminate extends javax.swing.JFrame {
public ProgressBarIndeterminate() {
    initComponents();
}


private void initComponents() {

    jProgressBar1 = new javax.swing.JProgressBar();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setAlwaysOnTop(true);
    setBackground(new java.awt.Color(255, 255, 255));
    setUndecorated(true);
    setResizable(false);

    jProgressBar1.setBackground(new java.awt.Color(255, 255, 255));
    jProgressBar1.setForeground(new java.awt.Color(153, 255, 0));
    jProgressBar1.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
    jProgressBar1.setFocusable(false);
    jProgressBar1.setIndeterminate(true);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
    );

    pack();
}
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new ProgressBarIndeterminate().setVisible(true);
        }
    });
}                        
private javax.swing.JProgressBar jProgressBar1;
}

Class ViewRec:

package mainform;
public class ViewRec extends javax.swing.JFrame {
public ViewRec() {
    initComponents();
    try{
        Thread.sleep(10000);
    }
    catch(Exception e){}
}  

private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    jTable1 = new javax.swing.JTable();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setBackground(new java.awt.Color(255, 255, 255));
    setResizable(false);

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {

        },
        new String [] {
            "E-Mail ID", "Customer Name", "City", "State", "Industry", "Priority"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
        };
        boolean[] canEdit = new boolean [] {
            false, false, false, false, false, false
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    jScrollPane1.setViewportView(jTable1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 912, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
    );

    pack();
}                            
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;    
}

Class ComposeMail:

package mainform;
public class ComposeMail extends javax.swing.JFrame {
ProgressBarIndeterminate pbi=new ProgressBarIndeterminate();

public ComposeMail() {                
        initComponents();            
}


private void initComponents() {

    jButton4 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    jButton4.setText("View Recipients");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton4ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()

                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(10, 10, 10)
    ))))));
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(11, 11, 11)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(3, 3, 3)                        
                .addComponent(jButton4))                
    )));
    pack();
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    pbi.setVisible(true);
    new ViewRec().setVisible(true);
    pbi.dispose();
}                                        
public static void main(String[] args){        
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new ComposeMail().setVisible(true);
        }
    });
}


private javax.swing.JButton jButton4;             
}

编译并运行ComposeMail以重新创建我的问题。

1 个答案:

答案 0 :(得分:1)

您的进度条会显示 - 如果只有Swing是线程安全的。

删除pbi.dispose(),您应该会看到进度条。

您正在将其设置为可见,但之后您立即创建了一个ViewRec实例 - 而ViewRec将阻止线程执行该操作。构建ViewRec之后,将调用setVisible(),并立即再次处理进度条。

您可以在Oracle提供的official Tutorials中找到有关如何使用进度条的信息。