【问题标题】:How to make a splash screen for GUI?如何为 GUI 制作启动画面?
【发布时间】:2013-04-14 14:43:28
【问题描述】:

您好,我是 Java 图形用户界面的新手,我试图让启动画面或图像出现 3 秒钟。然后它会进入我的主程序。有谁知道如何做到这一点,或者可以将我链接到任何教程?

到目前为止,我已经这样做了,但不知道从哪里开始。

public static void main(String[] args)
{
    splashInit();           // initialize splash overlay drawing parameters
    appInit();              // simulate what an application would do 
}

【问题讨论】:

  • 试试 this linkthis example
  • 最简单的方法是在显示应用程序之前让线程休眠 3000 毫秒。 Thread.sleep(3000);
  • @GnomezGrave:如果您不想踩到摇摆事件线程,最好使用摇摆计时器。

标签: java swing user-interface awt splash-screen


【解决方案1】:

最简单的方法是创建JFrame 并在其上添加您的screen,然后使用Thread.Sleep(long millies)

试试这个代码:

JWindow window = new JWindow();
window.getContentPane().add(
    new JLabel("", new ImageIcon(new URL("http://docs.oracle.com/javase/tutorial/uiswing/examples/misc/SplashDemoProject/src/misc/images/splash.gif")), SwingConstants.CENTER));
window.setBounds(500, 150, 300, 200);
window.setVisible(true);
try {
    Thread.sleep(5000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
window.setVisible(false);
JFrame frame = new JFrame();
frame.add(new JLabel("Welcome"));
frame.setVisible(true);
frame.setSize(300,100);
window.dispose();

或者你可以Create a Splash Screen使用SplashScreen

【讨论】:

  • 正确的想法,错误的方向,永远不要在 EDT 内调用 Thread.sleep 或从 EDT 外部创建/修改 Swing UI 组件
  • @MadProgrammer 公平地说:官方教程还在主线程上创建 UI :-/ 我从未使用过启动画面,但想知道在 EDT 上创建它时的正确交互,考虑到renderSplashFrame 的外观、实现和调用方式……在SplashDemo.java 示例中调用……
  • @Marco13 是的,它创造了一个多么美妙的混乱世界 - 有一篇精彩的博客文章回顾了这个问题的历史,但不用说,它在同一点结束,使用 EventQueue.invokeLater 来启动 UI,因为您无法确定调用了哪个线程 main。自从我使用闪屏已经有一段时间了,但我相信它会在 EDT 之外运行(启动它需要很长时间,并且可能会导致竞争条件),所以规则不同,因为一致性很容易??
【解决方案2】:

另请参阅How to Create a Splash Screen,了解基于 AWT 的启动功能。

【讨论】:

    【解决方案3】:

    这对我来说很好用。getScreenSize()、getWidth() 和 getHeight() 等函数可以用自己的值替换。

    public class splash extends JWindow 
    {
      public splash()
      {
         JWindow j=new JWindow();
    
         Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
    
         Icon img= new ImageIcon(this.getClass().getResource("2.jpg"));
         JLabel label = new JLabel(img);
         label.setSize(200,300);
         j.getContentPane().add(label);
         j.setBounds(((int)d.getWidth()-722)/2,((int)d.getHeight()-401)/2,722,401);
         j.setVisible(true);
         try
         {
            Thread.sleep(6000);
         }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
         j.setVisible(false);
    
      }
      public static void main(String[] args)
      {
        splash s=new splash();
      }
    }
    

    【讨论】:

      【解决方案4】:

      我使用此代码。也许你需要改变一些部分:

      import javax.swing.*;
      import java.awt.*;
      
      public class SplashScreen {
          private final JWindow window;
          private long startTime;
          private int minimumMilliseconds;
      
          public SplashScreen() {
              window = new JWindow();
              var image = new ImageIcon("C:\\example.jpg");
              window.getContentPane().add(new JLabel("", image, SwingConstants.CENTER));
              Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
              window.setBounds((int) ((screenSize.getWidth() - image.getIconWidth()) / 2),
                      (int) ((screenSize.getHeight() - image.getIconHeight()) / 2),
                      image.getIconWidth(), image.getIconHeight());
          }
      
          public void show(int minimumMilliseconds) {
              this.minimumMilliseconds = minimumMilliseconds;
              window.setVisible(true);
              startTime = System.currentTimeMillis();
          }
      
          public void hide() {
              long elapsedTime = System.currentTimeMillis() - startTime;
              try {
                  Thread.sleep(Math.max(minimumMilliseconds - elapsedTime, 0));
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              window.setVisible(false);
          }
      }
      

      下面是如何使用它:

          var splash = new SplashScreen();
          splash.show(2000);
      
          // Initializing...
      
          splash.hide();
      

      这将显示至少 2 秒的飞溅。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-04
        • 2014-08-02
        • 1970-01-01
        相关资源
        最近更新 更多