我的代码需要帮助。
我在启动 Java 应用程序的 SplashScreen 时遇到问题。
主要类 (bolaoJFrame) 扩展 JFrame
并调用扩展 JDialog
的屏幕登录。用户验证后,会创建 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);
}
});
}
启动画面类:
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();
}
最佳答案
我的第一个猜测是 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
在使启动屏幕可见和主体
之间插入短暂的延迟
虽然无法从您的代码片段中确定,但您也有可能阻塞事件调度线程,阻止处理任何新事件并阻止显示窗口
关于java - SplashScreen java 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24397717/