2016-01-20 29 views
0

我有一個問題。我的飛濺程序沒有運行。請原諒我,我是新和不能把代碼properly.i想想我的計時器代碼是錯誤的。任何幫助將不勝感激。我與數據庫的連接工作得很好。進度不開始定時器在Java中

我潑代碼

package Splash_package; 

import DataConnection.connection; 
import Mainframe.main_frame; 
import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import javax.swing.JOptionPane; 
import javax.swing.Timer; 

/** 
* 
* @author Aaqib Jan 
*/ 

public class Splash extends javax.swing.JFrame { 

    /** 
    * Creates new form splash 
    */ 
    public Splash() { 
     initComponents(); 

    } 
    connection cn=new connection(); 

    Timer tm = new Timer(500, new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent ae) { 
      count+=5; 
       if(count==10) 
        { 
         jLabel2.setText("Initializing"); 
         jProgressBar1.setStringPainted(true); 
         jProgressBar1.setValue(10); 
        } 
       if (count == 20) { 
        try { 
        Class.forName(cn.classname); 
        jLabel2.setText("Loading ..."); 
        jProgressBar1.setValue(20); 
         } catch (Exception ex) { 
       JOptionPane.showMessageDialog(null, ex.getStackTrace()); 
       System.exit(0); 

      } 
       } 
       if (count == 30) { 
        try { 
        cn.con = DriverManager.getConnection(cn.url); 
        jLabel2.setText("Checking database ..."); 
        jProgressBar1.setValue(30); 
        } catch (Exception ex) { 
       JOptionPane.showMessageDialog(null, ex.getStackTrace()); 
System.exit(0); 
      } 

       } 
       if (count == 40) { 
        try { 
        cn.st = cn.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 
        jLabel2.setText("Finding resources ..."); 
        jProgressBar1.setValue(40); 
        } catch (Exception ex) { 
       JOptionPane.showMessageDialog(null, ex.getStackTrace()); 
System.exit(0); 
      } 

       } 
       if (count == 50) { 
        try { 
        cn.rs = cn.st.executeQuery("select * from usertable"); 
        jLabel2.setText("Checking Errors ..."); 
        jProgressBar1.setValue(50); 
         } catch (Exception ex) { 
       JOptionPane.showMessageDialog(null, ex.getStackTrace()); 
System.exit(0); 
      } 

       } 
       if (count == 60) { 
        try { 
        cn.rs = cn.st.executeQuery("select * from labourinfo"); 
        jLabel2.setText("No Error Getting ..."); 
        jProgressBar1.setValue(60); 
    } catch (Exception ex) { 
       JOptionPane.showMessageDialog(null, ex.getStackTrace()); 
System.exit(0); 
      } 
       } 
       if (count == 70) { 
        try { 
        cn.rs = cn.st.executeQuery("select * from salarygrade"); 
        jLabel2.setText("Checking Modules ..."); 
        jProgressBar1.setValue(70); 
    } catch (Exception ex) { 
       JOptionPane.showMessageDialog(null, ex.getStackTrace()); 
System.exit(0); 
      } 
       } 
       if (count == 80) { 
        try { 
        cn.rs = cn.st.executeQuery("select * from labourattendence"); 
        jLabel2.setText("Almost Finish ..."); 
        jProgressBar1.setValue(80); 
    } catch (Exception ex) { 
       JOptionPane.showMessageDialog(null, ex.getStackTrace()); 
System.exit(0); 
      } 
       } 
       if (count == 90) { 
        try { 
        cn.rs = cn.st.executeQuery("select * from labourattendence"); 
        jLabel2.setText("Starting ..."); 
        jProgressBar1.setValue(90); 
    } catch (Exception ex) { 
       JOptionPane.showMessageDialog(null, ex.getStackTrace()); 

      } 
       } 
       if(count==95){ 
        jProgressBar1.setValue(100); 
       } 
       if (count == 100) { 
        main_frame main = new main_frame(); 
        main.show(); 
        dispose(); 
       } 



     } 

    }); 


    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jProgressBar1 = new javax.swing.JProgressBar(); 
     jLabel1 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jLabel1.setText("jLabel1"); 

     jLabel2.setText("jLabel2"); 

     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() 
         .addGap(207, 207, 207) 
         .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(89, 89, 89) 
         .addComponent(jLabel1))) 
       .addContainerGap(307, Short.MAX_VALUE)) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addGap(0, 0, Short.MAX_VALUE) 
       .addComponent(jLabel2) 
       .addGap(217, 217, 217)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(57, 57, 57) 
       .addComponent(jLabel2) 
       .addGap(37, 37, 37) 
       .addComponent(jLabel1) 
       .addGap(59, 59, 59) 
       .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(230, Short.MAX_VALUE)) 
     ); 

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

    private void formWindowOpened(java.awt.event.WindowEvent evt) {         
     this.setLocationRelativeTo(null); 
     this.setBackground(new Color(0, 255, 0, 0)); 
     tm.start(); 
    }         

    /** 
    * @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(Splash.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(Splash.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(Splash.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(Splash.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

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







// Variables declaration - do not modify      
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
     private javax.swing.JProgressBar jProgressBar1; 
     // End of variables declaration     
     int count=0; 

    } 

我的主類

import Splash_package.Splash; 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

public class Main_Class { 



    public static void main(String[] args) { 



    new Splash().setVisible(true); 

} 
} 
+0

您可能希望有[併發在Swing(http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)和[工作線程和SwingWorker類(HTTP的讀取://文檔。 oracle.com/javase/tutorial/uiswing/concurrency/worker.html)。擺動'Timer'是不會做你想做的 – MadProgrammer

回答

0

下面的方法不會被調用,這將導致定時器不會開始:

private void formWindowOpened(java.awt.event.WindowEvent evt) {         
    this.setLocationRelativeTo(null); 
    this.setBackground(new Color(0, 255, 0, 0)); 
    tm.start(); 
}   

移動tm.start ()如下:

public Splash() { 
    initComponents();   
    tm.start(); 
}   
+0

嘿非常感謝............它幫助了很多人。你的救星。 –