如何制作 GUI 的启动屏幕?

问题描述 投票:0回答:6

您好,我是 Java 中的 GUI 新手,正在尝试使启动屏幕或图像显示 3 秒。然后它将进入我的主程序。有人知道如何做到这一点或者可以将我链接到任何教程吗?

到目前为止我已经做到了这一点,但不知道接下来该去哪里。

public static void main(String[] args)
{
    splashInit();           // initialize splash overlay drawing parameters
    appInit();              // simulate what an application would do 
}
java swing user-interface awt splash-screen
6个回答
11
投票

最简单的一种,是创建

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();

或者您可以使用 SplashScreen创建启动画面


11
投票
另请参阅

如何创建启动屏幕以了解基于 AWT 的启动画面功能。

splash image


0
投票
要在启动屏幕上打印消息,您需要将图片添加到应用程序并将其添加到manifest.mf中:

SplashScreen-Image: path/picture.jpg
然后使用这样的代码:

private static Graphics2D splashGraphics = null; private static SplashScreen splash; private static int dpi; public static void main(String[] args) { initSplashMessages(); splashMessage("Start program..."); ... splashMessage("Load data..."); ... } private static void initSplashMessages() { splash = SplashScreen.getSplashScreen(); if (splash == null) return; splashGraphics = splash.createGraphics(); if (splashGraphics == null) return; dpi = Toolkit.getDefaultToolkit().getScreenResolution(); } public static void splashMessage(String message) { if (splashGraphics == null) return; Dimension dim = splash.getSize(); splashGraphics.setColor(Color.LIGHT_GRAY); splashGraphics.fillRect(0, dim.height - dpiX(20), dim.width, dpiX(20)); splashGraphics.setPaintMode(); splashGraphics.setColor(Color.BLACK); splashGraphics.setFont(new Font("Arial",Font.PLAIN, dpiX(12))); splashGraphics.drawString(message, dpiX(3), dim.height - dpiX(5)); splash.update(); } private static int dpiX(int x) { return (int) Math.round(1.0 * x * dpi / 96); }
在 IntelliJ 中将 picture.jpg 添加到您的项目中(使用文件):

为了调试,请使用“修改选项”/“添加虚拟机选项”并写入 -splash:路径/图片.jpg


0
投票
我创建了一个与静态方法一起使用的类:

public class SplashScreen { public static final JWindow SPLASH_SCREEN; static { SPLASH_SCREEN = new JWindow(); } public static void show() { SPLASH_SCREEN.getContentPane().add( new JLabel("Loading..." , SwingConstants.CENTER)); SPLASH_SCREEN.setSize(300, 200); SPLASH_SCREEN.setLocationRelativeTo(null); SPLASH_SCREEN.setVisible(true); } public static void close() { SPLASH_SCREEN.dispose(); } }

main()

public static void main(String[] args) { SplashScreen.show(); final Controller controller; try { controller = loadController(); } finally { SplashScreen.close(); } ... }
    

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

-2
投票
我使用这个代码。也许你需要改变一些部分:

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 秒的飞溅。

© www.soinside.com 2019 - 2024. All rights reserved.