2011-05-13 62 views
0

我試圖讓做一些文字出現在我的小應用程序的負載,所以我做了一個簡單的SSCCE(.ORG)前:Java的閃屏

import java.awt.*; 
import javax.swing.*; 

    public class test extends JApplet { 
     public void init() { 

       this.add(new JLabel("Button 1")); 
       System.out.println("Hello world..."); 


       try { 
       Thread.sleep(3000); 
       }catch(Exception hapa) { hapa.printStackTrace(); } 


     } 
    } 

如果你運行它,按鈕1將出現在3秒之後,當它出現在......我做錯了什麼?

回答

1

JustinKSU涵蓋了問題的技術部分。

更好的策略是在小應用程序出現之前使用imageparam來顯示'splash'。有關更多詳細信息,請參閱Special Attributes of Applets

我想要一個固定的時間......不只是加載。

在這種情況下,將CardLayout放在小程序中。將「飛濺」添加到第一張卡,其餘的GUI添加到另一張。在init()的末尾創建一個非重複的Swing Timer,它將翻轉到具有主GUI的卡片。

E.G.

// <applet code='SplashApplet' width='400' height='400'></applet> 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class SplashApplet extends JApplet { 

    public void init() { 
     final CardLayout cardLayout = new CardLayout(); 
     final JPanel gui = new JPanel(cardLayout); 

     JPanel splash = new JPanel(); 
     splash.setBackground(Color.RED); 
     gui.add(splash, "splash"); 

     JPanel mainGui = new JPanel(); 
     mainGui.setBackground(Color.GREEN); 
     gui.add(mainGui, "main"); 

     ActionListener listener = new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
       cardLayout.show(gui, "main"); 
      } 
     }; 

     Timer timer = new Timer(3000, listener); 
     // only needs to be done once 
     timer.setRepeats(false); 
     setContentPane(gui); 
     validate(); 
     timer.start(); 
    } 
} 
+0

是的,但我想要一個固定的時間......不只是加載。 – nn2 2011-05-13 03:13:54

+0

看到編輯... – 2011-05-13 03:27:07

+0

@Jakhr:代碼中有一個小錯誤,忽略關閉定時器。查看單行註釋的編輯。 – 2011-05-13 05:21:04

2

我認爲init()方法必須在渲染項目之前返回。

+0

你覺得沒錯。 – 2011-05-13 02:48:44