【问题标题】:splash screen picture doesn’t appear [closed]启动画面图片不出现[关闭]
【发布时间】:2013-02-18 05:47:49
【问题描述】:

在我的程序中,我想用启动画面启动它几秒钟,然后启动程序的第一帧...

但是有两个问题。

首先出现启动画面,但本应在其中显示的照片没有出现

第二次启动结束时,第一帧开始了,但它没有。

这是我的代码.....所以我需要知道问题出在哪里?

public class Splash extends JWindow {

    AbsoluteLayout absoluto;
    AbsoluteConstraints absimage,absrra;
    ImageIcon Image;
    JLabel jlabel;
    JProgressBar Barra;
    public Splash(){
         absoluto=new AbsoluteLayout();
         absimage = new AbsoluteConstraints(0,0);
         absrra = new AbsoluteConstraints(0,410);
         jlabel=new JLabel();
         Image=new ImageIcon("sales.png");
         jlabel.setIcon(Image);
         Barra=new JProgressBar();
         Barra.setPreferredSize(new Dimension(410,10));
         this.getContentPane().setLayout(absoluto);
         this.getContentPane().add(jlabel,absimage);
         this.getContentPane().add(Barra,absrra);
         new  Thread(){
             public void run(){
                 int i=0;
                 while(i<101){
                     Barra.setValue(i);
                     i++;
                     try {
                         sleep(30);
                     } catch (InterruptedException ex) {
                       //  Logger.getLogger(Splash.class.getName()).log(Level.SEVERE,null,ex);
                     }
                 }
                 System.exit(0);
             }
         }.start();
         this.pack();
         this.setLocationRelativeTo(null);
         this.setVisible(true);
    }

    public static void main (String args[]) throws SQLException, ClassNotFoundException{
        new Splash();
        loginForm a =new loginForm();
        a.setTitle("fram 1");
        a.setSize(700,600);
        a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        a.setLocationRelativeTo(null);
        a.setVisible(true);
    }
}

【问题讨论】:

  • 您可以先查看thisthis 的几个示例

标签: java image swing splash-screen


【解决方案1】:

如果不知道更多,我会说你遇到了一系列问题......

首先,这个...

Image=new ImageIcon("sales.png");

ImageIcon(String) 需要一个文件(来自本地磁盘),在这里你说图像必须存在于执行程序的同一目录中。如果图像是嵌入式资源,您将遇到麻烦。您需要提供URL

其次,我不知道 AbsoluteLayout 是什么,但我怀疑这是一种方便的说法 setLayout(null) 并且没有证据证明,我怀疑你没有提供任何宽度/高度标签的信息,这意味着它将为 0x0。

您可以查看this,了解使用JLabel 作为其他组件的后备容器的示例。

您还违反了 Swing 的单线程规则,即更新 EDT 外侧的进度条。这是一个很大的,不,不。实际上,您最好使用SwingWorker

查看Concurrency in Swing了解更多详情。

以下是同一问题的几个不同示例。

回答您问题的第二部分...

我会仔细研究System.exit(0); ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    相关资源
    最近更新 更多