2015-05-22 27 views
0

/* * 要改變這種許可證頭,選擇在項目屬性許可頭。 *要更改此模板文件,請選擇工具|模板 *並在編輯器中打開模板。 */關閉一個閃屏在揮杆

package newpackage; 

import javax.swing.JProgressBar; 

/** 
* 
* @author Pavilion 
*/ 
public class Progress extends javax.swing.JFrame { 

    /** 
    * Creates new form Progress 
    */ 
    public Progress() { 
     initComponents(); 

     Progressloader pl=new Progressloader(jProgressBar2); 

     // try { 
     // pl.join(); 
     // } catch (InterruptedException ex) { 
     // new Homescreen().setVisible(true); 
     // } 
     //System.exit(0); 

     dispose(); 

    } 

    // public void shut(){ 
    // dispose(); 
    //} 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jPanel1 = new javax.swing.JPanel(); 
     jLabel1 = new javax.swing.JLabel(); 
     jPanel2 = new javax.swing.JPanel(); 
     jProgressBar2 = new javax.swing.JProgressBar(); 
     jPanel3 = new javax.swing.JPanel(); 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTextArea1 = new javax.swing.JTextArea(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     setUndecorated(true); 
     getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); 

     jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/newpackage/above-progress-bar-image_1.png"))); // NOI18N 
     jLabel1.setText("jLabel1"); 

     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(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 561, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(1, 1, 1)) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE) 
     ); 

     getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, 320)); 

     jProgressBar2.setValue(50); 

     javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); 
     jPanel2.setLayout(jPanel2Layout); 
     jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jProgressBar2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE) 
     ); 
     jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel2Layout.createSequentialGroup() 
       .addComponent(jProgressBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(0, 0, Short.MAX_VALUE)) 
     ); 

     getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 320, 560, 5)); 

     jTextArea1.setEditable(false); 
     jTextArea1.setBackground(new java.awt.Color(51, 51, 51)); 
     jTextArea1.setColumns(20); 
     jTextArea1.setFont(new java.awt.Font("Arial", 0, 8)); // NOI18N 
     jTextArea1.setForeground(new java.awt.Color(255, 255, 255)); 
     jTextArea1.setLineWrap(true); 
     jTextArea1.setRows(3); 
     jTextArea1.setText("XOD SECURE is based on software from XOD Technologies Inc. which has been licensed under the Common Development and Distribution License(CDDL) and the GNU general public license number 2 by the Indian Government. This software has been solely made for the use of the Inadian Police as well as the CPRF. Any use of the software for any other purpose and improper distribution will be an illegal action and serious action will be taken against the person."); 
     jScrollPane1.setViewportView(jTextArea1); 

     javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); 
     jPanel3.setLayout(jPanel3Layout); 
     jPanel3Layout.setHorizontalGroup(
      jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE) 
     ); 
     jPanel3Layout.setVerticalGroup(
      jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel3Layout.createSequentialGroup() 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(0, 4, Short.MAX_VALUE)) 
     ); 

     getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 325, 560, 40)); 

     pack(); 
     setLocationRelativeTo(null); 
    }// </editor-fold>       

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(Progress.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(Progress.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(Progress.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(Progress.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

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

    // Variables declaration - do not modify      
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JPanel jPanel2; 
    private javax.swing.JPanel jPanel3; 
    private javax.swing.JProgressBar jProgressBar2; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTextArea jTextArea1; 
    // End of variables declaration     
} 

class Progressloader extends Thread{ 
    JProgressBar jpb; 

    Progressloader(JProgressBar jpb){ 
     this.jpb=jpb; 
     start(); 
    } 

    @Override 
    public void run() 
    { 

     for(int i=jpb.getMinimum();i<=jpb.getMaximum();i++) 
     { 
      jpb.setValue(i); 
      try 
      { 
      Thread.sleep(20); 
      }catch(InterruptedException ex){} 

     } 
      new Homescreen().setVisible(true); 
     //new Progress().setVisible(false); 

    } 
} 

這有什麼代碼問題?進度條正常運行和新窗口打開,但我不能夠處理的閃屏

回答

2

你違反了燎螺紋規擺動,開始時,你應該使用SwingWorker,其提供可以使用安全地更新從用戶界面的方法。

詳情請參閱Concurrency in SwingWorker Threads and SwingWorker

例如:

你的主要問題是,你的ProgressLoader有沒有提到閃屏的事實,所以不能將其關閉。

SwingWorker提供PropertyChangeListener,你可以用它來監視工人,當它的DONE,你可以用它來關閉該窗口

的狀態,這裏的技巧是創建從你內在的SwingWorker「閃屏」,並添加一個PropertyChangeListener來啓動它作爲一個匿名類,這樣當它這樣做,你可以調用disposePropertyChangeListener內之前。

從概念上說,在這裏展示:Using SwingWorker to add a progress bar in a GUI