【问题标题】:swings trouble with splash screen闪屏出现问题
【发布时间】:2014-07-25 01:29:21
【问题描述】:

我正在尝试在我的主要活动之后实现启动画面。下面的代码是我到目前为止所做的。而且我无法让我的启动画面正常工作。请帮忙。 在运行文件时,它显示 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();
    }
}

【问题讨论】:

  • SplashScreen 是如何实现的?
  • 如何实施?我想添加一张图片,但不想添加。你能帮忙吗
  • 既然可以称呼它我猜你已经写了一些部分,你可以将它添加到问题中吗?当你已经拥有它的某些部分时,很难从头开始编写它
  • 你应该把 renderSplashFrame 放在你的启动画面类中
  • SplashScreen 有一些可能性。顺便说一句,所有的都是旧的 awt,而不是新的 swing GUI。

标签: java swing splash-screen


【解决方案1】:

创建您的初始屏幕图像,例如 MySplashyScreen.gif,并将其放在名为 images 的目录中(或任何您喜欢的位置)。

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

java -splash:images/MySplashyScreen.gif SplashDemo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多