【问题标题】:Issue with SplashScreen javaSplashScreen java的问题
【发布时间】:2014-06-24 22:59:44
【问题描述】:

我的代码需要帮助。

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

当类主体加载未完成时,SplashScreen 应在验证后显示JProgressBar

这是主要类的主要方法:

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

【问题讨论】:

  • 欢迎来到 Stack Overflow!避免使用null 布局。您无法控制影响在不同平台/操作系统上改变其尺寸的组件的尺寸要求/决定的因素

标签: java swing splash-screen


【解决方案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 之间插入一小段延迟

虽然无法从您的代码 sn-p 中确定,但也有可能您正在阻塞事件调度线程,阻止处理任何新事件并阻止显示窗口

【讨论】:

  • 伙计们感谢您的帮助,但实际上 SplashScreen 仅在 bolaoJFrame 也获得可见性时才会出现。如果您注意到,SplashScreen 是在获得可见性之前由 bolaoJFrame 创建的。流程为:bolaoJFrame创建>>登录界面由bolaoJFrame创建>>SplashScreen由bolaoJFrame创建
  • 为什么会以其他方式发生?在您退出该方法之前,EDT 无法处理所有新事件以使您的 UI 跟上速度
  • 我不明白。你能解释得更好吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 2014-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多