用闪屏摇摆麻烦

时间:2014-06-04 11:55:16

标签: java swing

我正在尝试按照我的主要活动实现启动画面。下面的代码是我到目前为止所做的事情。我无法让我的启动画面工作。请帮忙。 在运行该文件时,它显示SplashScreen.getSplashScreen()返回null。该怎么办?

SplashDemo.java

public class SplashDemo extends Frame implements ActionListener {
    static void renderSplashFrame(Graphics2D g, int frame) {
        final String[] comps = {"foo", "bar", "baz"};
        g.setComposite(AlphaComposite.Clear);
        g.fillRect(120,140,200,40);
        g.setPaintMode();
        g.setColor(Color.BLACK);
        g.drawString("Loading "+comps[(frame/5)%3]+"...", 120, 150);
    }
    public SplashDemo() {
        super("SplashScreen demo");
        setSize(300, 200);
        setLayout(new BorderLayout());
        Menu m1 = new Menu("File");
        MenuItem mi1 = new MenuItem("Exit");
        m1.add(mi1);
        mi1.addActionListener(this);
        this.addWindowListener(closeWindow);

        MenuBar mb = new MenuBar();
        setMenuBar(mb);
        mb.add(m1);
        final SplashScreen splash = SplashScreen.getSplashScreen();
        if (splash == null) {
            System.out.println("SplashScreen.getSplashScreen() returned null");
            return;
        }
        Graphics2D g = splash.createGraphics();
        if (g == null) {
            System.out.println("g is null");
            return;
        }
        for(int i=0; i<100; i++) {
            renderSplashFrame(g, i);
            splash.update();
            try {
                Thread.sleep(90);
            }
            catch(InterruptedException e) {
            }
        }
        splash.close();
        setVisible(true);
        toFront();
    }
    public void actionPerformed(ActionEvent ae) {
        System.exit(0);
    }

    private static WindowListener closeWindow = new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            e.getWindow().dispose();
        }
    };

    public static void main (String args[]) {
        SplashDemo test = new SplashDemo();
    }
}

1 个答案:

答案 0 :(得分:0)

创建你的初始屏幕图片,比如说MySplashyScreen.gif,并把它放在一个名为images(或你喜欢的任何地方)的目录中。

然后在启动时使用此命令行:

java -splash:images / MySplashyScreen.gif SplashDemo