加载主窗体java后,进度条不会关闭

时间:2013-12-04 07:22:18

标签: java swing

主类是:不包括netbean隐藏代码 我首先从主类加载进度条类,然后在第二类Name SplashScreen中完成进度条后关闭..

package im;

import java.awt.event.WindowListener;

/**
 *
 * @author Ch. Virk
 */
public class frmMain extends javax.swing.JFrame {


    /**
     * Creates new form frmMain
     */
    public frmMain() {



       initComponents();


    }


    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

                SplashScreen execute;
                execute = new SplashScreen();
    }


}

第二课:

package im;

/**
 *
 * @author Ch. Virk
 */
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionListener;
import javax.swing.*;

public class SplashScreen extends JWindow {

    static boolean isRegistered;
    private static final JProgressBar progressBar = new JProgressBar();
    private static SplashScreen execute;
    private static int count;
    private static Timer timer1;

    public SplashScreen() {

        Container container = getContentPane();
        container.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBorder(new javax.swing.border.EtchedBorder());
        panel.setBackground(new Color(255, 255, 255));
        panel.setBounds(10, 10, 348, 150);
        panel.setLayout(null);
        container.add(panel);

        JLabel label = new JLabel("Load Inventory Manager!");
        label.setFont(new Font("Verdana", Font.BOLD, 14));
        label.setBounds(85, 25, 280, 30);
        panel.add(label);

        progressBar.setMaximum(100);
        progressBar.setBounds(55, 180, 250, 15);
        container.add(progressBar);
        loadProgressBar();
        setSize(370, 215);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void loadProgressBar() {
        ActionListener al;
        al = new ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                count++;

                progressBar.setValue(count);

                System.out.println(count);

                if (count == 100) {

                    createFrame();

                    execute.setVisible(false);//swapped this around with timer1.stop()
                    timer1.stop();

                }

            }
            private void createFrame() throws HeadlessException {
                frmMain mainForm = new frmMain();
                mainForm.setVisible(true);
            }



        };
        timer1 = new Timer(50, al);
        timer1.start();
    }





    public static void main(String[] args) {

        execute = new SplashScreen();
    }
}

显示错误: * 错误出现在此行代码execute.setVisible(false); *

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at im.SplashScreen$1.actionPerformed(SplashScreen.java:69)
    at javax.swing.Timer.fireActionPerformed(Timer.java:312)
    at javax.swing.Timer$DoPostEvent.run(Timer.java:244)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

2 个答案:

答案 0 :(得分:2)

您正在构造函数中调用一个函数(技术上由计时器线程启动),该函数试图使用变量execute,在构造函数完成执行之前,该函数实际上并未分配。这就是为什么值为null,并且您将获得异常。

您可以重新设计您的类以避免这种情况,或者更改引用以引用SplashScreen.this来引用包含外部类的实例。

答案 1 :(得分:0)

当没有为其分配任何内容时,切勿使用带有函数的变量。这就是你所做的,最终会出现错误。