SplashScreen java的问题

时间:2014-06-24 22:59:45

标签: java swing splash-screen splash

我的代码需要帮助。

我在启动Java应用程序的SplashScreen时遇到问题。 扩展JFrame并调用扩展JDialog的屏幕登录的主类(bolaoJFrame)。用户验证后,SplashScreen已创建,但不会显示。 我究竟做错了什么?

SplashScreen应在验证后显示,以显示JProgressBar,而班主任的加载未完成。

这是主要类的metodo主要内容:

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(bolaoJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(bolaoJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(bolaoJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(bolaoJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>


    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {

            bolaoJFrame principal= new bolaoJFrame();
            principal.setLocationRelativeTo(null);
            login = new loginJDialog(principal, true);
            if(newUser==true){
                CardLayout cards = (CardLayout)(principal.jpPrincipal.getLayout());
                cards.show(principal.jpPrincipal, "cadastro");
            }else{
                if(Usuario.userCriado==0){
                    System.exit(0);
                }else{

                    Splash telaSplash =new Splash(null);
                    telaSplash.setVisible(true);

                    CardLayout cards = (CardLayout)(principal.jpPrincipal.getLayout());
                    cards.show(principal.jpPrincipal, "apostas");
                    principal.jLabel1.setText("Bem vindo " + user.getUsername());
                    principal.jLabel4.setIcon(new Util().dimencionaImagem(user.getFoto(),80));

                    HoraDoSistema.start(principal.retomaHora(),principal.lbRelagio);
                    principal.montaCards();
                    PanelRanking pr = new PanelRanking(10, 1, user.getUserCod());

                    principal.jpRanking.add(pr,"1");
                    if(pr.getPosUser()>0){
                       principal.lbRankingInfo.setText("Sua posição atual é " + pr.getPosUser() + "º" ); 
                    }
                    if(pr.getPontuacaoUser()>0){
                       principal.lbPontuacaoInfo.setText("Você fez " + pr.getPontuacaoUser()+ " pontos" ); 
                    }                        
                    principal.lbNext.requestFocus();


                    principal.verificaDisponibilidad();
                }
            }


          principal.setVisible(true); 




        }
    });
}

SplashScreen类:

public Splash(JFrame owner) {
    //super(owner);
    setLayout(null);
    Util.centralizarJanela(this, 521, 335);
    inicialize();
}



public void inicialize(){
    JLabel fundo = new JLabel();
    //fundo.setLocation(new Point(0,0));
    fundo.setIcon(new ImageIcon(getClass().getResource("/Principal/splash.png")));
    fundo.setSize(521, 315);
    add(fundo);

    barraDeProgresso= new JProgressBar();
    barraDeProgresso.setBackground(Color.red);
    barraDeProgresso.setBounds(0, 315, 521, 20);
    barraDeProgresso.setStringPainted(true);
    add(barraDeProgresso);
}

public void setValorBarraDeProgresso(int progresso){
    barraDeProgresso.setValue(progresso);
}

public int getValorBarraDeProgresso(){
    return barraDeProgresso.getValue();
}

1 个答案:

答案 0 :(得分:1)

我的第一个猜测是setLayout(null);,我的第二个猜测是你实际上没有在Splash窗口中应用大小或位置

通常情况下,pack()setLocationRelativeTo(bolaoJFrame)之类的内容会是您想要的,但由于您决定放弃布局管理器,pack将不再帮助您......

<强>更新...

所以基本的工作流程就像......

bolaoJFrame principal= new bolaoJFrame();
principal.setLocationRelativeTo(null);

//...
Splash telaSplash =new Splash(null);
telaSplash.setVisible(true);
//..

principal.setVisible(true); 

问题在于,由于setVisible命令之间没有延迟,窗口可能会(几乎)同时出现。

您可以使用javax.swing.Timer在启用闪屏和principal

之间插入一个短延迟

虽然无法从您的代码段确定,但您也可能阻止事件调度线程,阻止处理任何新事件并阻止窗口显示