Java 如何为GUI创建初始屏幕?

Java 如何为GUI创建初始屏幕?,java,swing,user-interface,awt,splash-screen,Java,Swing,User Interface,Awt,Splash Screen,大家好,我是Java中GUI的新手,我正试图让一个启动屏幕或一个图像出现3秒钟。然后,它将进入我的主程序。有没有人知道如何做到这一点,或者可以将我链接到任何教程 到目前为止,我已经做到了这一点,但不知道从这里走到哪里 public static void main(String[] args) { splashInit(); // initialize splash overlay drawing parameters appInit();

大家好,我是Java中GUI的新手,我正试图让一个启动屏幕或一个图像出现3秒钟。然后,它将进入我的主程序。有没有人知道如何做到这一点,或者可以将我链接到任何教程

到目前为止,我已经做到了这一点,但不知道从这里走到哪里

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

最简单的方法是创建
JFrame
并在其上添加
屏幕
,然后使用
Thread.Sleep(长毫秒)

请尝试以下代码:

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();
您可以使用类

另请参见,了解基于AWT的启动功能


这对我来说很好。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();
  }
}

我使用这个代码。也许您需要更改某些部分:

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秒钟的启动时间。

试试看,最简单的方法是在显示应用程序之前让线程休眠3000毫秒<代码>线程睡眠(3000)@GnomezGrave:如果不想踩到Swing事件线程,最好使用Swing计时器。正确的想法,错误的方向,永远不要在EDT内调用thread.sleep,或者从外部创建/修改Swing UI组件EDT@MadProgrammer平心而论:官方教程还在主线程上创建了UI:-/我从未使用过闪屏,但考虑到。。。在
SplashDemo.java
示例中,
renderSplashFrame
的外观、实现和调用方式都很不灵活…@Marco13是的,它创造了一个多么奇妙的混乱世界-有一篇精彩的博客文章,讲述了这个问题的历史,但不用说,它也在同一点上结束了,使用EventQueue.invokeLater启动UI,因为您无法确定调用了哪个线程main。我已经有一段时间没有使用闪屏了,但我相信它会脱离EDT(启动需要很长时间,可能会导致比赛条件),所以规则是不同的,因为一致性很容易