【问题标题】:Error in splash screen in Java/SwingJava/Swing 中的闪屏错误
【发布时间】:2017-08-29 16:20:50
【问题描述】:

我正在用 Java 创建新的启动画面(使用 Netbeans 作为我的 IDE)。

但问题是,在进度条达到 100% 后,它会自动移动到第二帧并退出显示初始屏幕的第一帧。但是这里第一屏的启动画面没有退出。

这是我的代码

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) {
        }
    }

【问题讨论】:

标签: java swing


【解决方案1】:

我曾经创造了一个简单的飞溅,不是为了幻想,但对我来说它成功了。

这是从 netbeans 整个类的复制粘贴,因此您需要更改一些内容。

这个例子你会得到一个带有图像和进度条的飞溅。

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                   
}

打电话

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

到最后

s.setVisible(false);

【讨论】:

    【解决方案2】:

    以下是我的代码,它在我的系统中正常工作。你可以尝试在你的代码中添加导入以下内容

    //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();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-08
      • 2018-07-14
      • 2014-01-10
      • 1970-01-01
      • 2018-05-27
      • 2019-05-14
      • 1970-01-01
      • 2020-10-04
      相关资源
      最近更新 更多