简体   繁体   中英

Error in splash screen in Java/Swing

I am creating new Splash screen in Java (using Netbeans as my IDE).

But the problem is that after reaching 100% by progress bar it is automatically move to 2nd frame and exit the 1st frame which is showing the splash screen. But here 1st screen of splash screen is not exiting.

This is my code

public NewJFrame() {
        initComponents();
        try {
            for (int i=0;i<=100;i++){
                Thread.sleep(40);
            jLabel1.setText(Integer.toString(i)+"%");
            jProgressBar2.setValue(i);
            if (i==100)
            {
                this.setVisible(false);
              new  NewJFrame1().setVisible(true);
            }
            }
        } catch (Exception e) {
        }
    }

I once created a simple splash, not to fancy but for me it did the trick.

This is a copy paste from the entire class from netbeans, so you will need to change somethings.

This example you will get a splash with a image and a progress bar bellow.

import javax.swing.SwingUtilities;

public class Splash extends javax.swing.JWindow {

private static final long serialVersionUID = -6711792976773608816L;

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

public void splashScreenInit() {
    setLocationRelativeTo(null);
    setProgressMax(100);
    setVisible(true);        
}

/**
 * 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("all")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    imageLabel = new javax.swing.JLabel();
    progressBar = new javax.swing.JProgressBar();

    imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/help/splash.png"))); // NOI18N
    getContentPane().add(imageLabel, java.awt.BorderLayout.CENTER);

    progressBar.setForeground(new java.awt.Color(51, 85, 112));
    progressBar.setStringPainted(true);
    getContentPane().add(progressBar, java.awt.BorderLayout.PAGE_END);

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

/**
 * Sets the max value for the progressbar
 * @param maxProgress 
 */
public void setProgressMax(int maxProgress) {
    progressBar.setMaximum(maxProgress);
}

public void setProgress(int progress) {
    final int theProgress = progress;
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            progressBar.setValue(theProgress);
        }
    });
}

/**
 * Changes the string message and set a new value fro the progressbar
 * @param message String the message
 * @param progress int the new value of the progressbar. This cant be higher than the value defined in the setProgressMax
 */
public void setProgress(String message, int progress) {
    final int theProgress = progress;
    final String theMessage = message;
    setProgress(progress);
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            progressBar.setValue(theProgress);
            setMessage(theMessage);
        }
    });
}

private void setMessage(String message) {
    if (message == null) {
        message = "";
        progressBar.setStringPainted(false);
    } else {
        progressBar.setStringPainted(true);
    }
    progressBar.setString(message);
}

// Variables declaration - do not modify                     
private javax.swing.JLabel imageLabel;
private javax.swing.JProgressBar progressBar;
// End of variables declaration                   
}

To call

final Splash s = new Splash();
SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        s.splashScreenInit();
    }
});

and in the end

s.setVisible(false);

the following is my code it is working properly in my system.. u can try add import the following in your codes

//import com.sun.awt.AWTUtilities;
//import java.awt.event.ActionEvent;
//import javax.swing.Timer;
//import javax.swing.UIManager;
//import java.awt.event.ActionListener;
//import javax.swing.JProgressBar;



    private Timer t;
        private ActionListener al;

        /**
         * Creates new form NewJFrame
         */

        public NewJFrame() {

            ActionListener a1 = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                  //  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                if(jProgressBar1.getValue()<100)
                    {
                  jProgressBar1.setValue(jProgressBar1.getValue()+5);
                    }
                else
                    {
                        t.stop();
                        abcd();
                    }
                }
            };
       t=new Timer(80, a1);
       initComponents();
       AWTUtilities.setWindowOpaque(this, false);
       t.start();
        }

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

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

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setUndecorated(true);

            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()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(123, 123, 123)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(92, 92, 92)
                            .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(141, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(66, 66, 66)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(41, 41, 41)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(133, Short.MAX_VALUE))
            );

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );

            pack();
        }// </editor-fold>                        
    private void abcd()
    {
        NewJFrame1 f1= new NewJFrame1();
        f1.setLocationRelativeTo(null);
    f1.setVisible(true);
    this.dispose();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM