0

我试图让一些文本在我的小程序加载之前出现,所以我做了一个简单的 SSCCE(.org):

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 秒后出现,而它应该在此之前出现......我做错了什么?

4

2 回答 2

2

我认为该init()方法必须在呈现项目之前返回。

于 2011-05-13T02:21:26.503 回答
1

JustinKSU 涵盖了问题的技术部分。

更好的策略是image param在小程序出现之前使用 来显示“飞溅”。有关详细信息,请参阅小程序的特殊属性

我想要一个固定的时间......不仅仅是加载。

在这种情况下,将 aCardLayout放入小程序中。将“splash”添加到第一张卡,GUI 的其余部分添加到另一张卡。在init()创建一个非重复的 Swing结束时Timer,它将翻转到带有主 GUI 的卡片。

例如

// <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();
    }
}
于 2011-05-13T02:48:19.937 回答