Progressbar不是以Java中的Timer开头的

时间:2016-01-20 18:19:18

标签: java swing timer

我有问题。我的启动程序没有运行。请原谅我,因为我是新的,不能正确地把代码。我认为我的计时器代码是错误的。任何帮助将不胜感激。我与数据库的连接工作正常。

我的启动代码

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);

}
}

1 个答案:

答案 0 :(得分: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();
}