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

  • 本文关键字:窗体 java 加载 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();
    }
}

显示错误:*这一行代码执行时出错。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)

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

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

当一个变量没有任何赋值时,千万不要将它与函数一起使用。这就是你所做的,它最终会出错。

最新更新