我的 java 应用程序的动态启动屏幕

发布于 2024-11-10 09:54:38 字数 148 浏览 3 评论 0原文

我想为我的 Java 应用程序创建一个启动屏幕。我设法使用 NetBeans 默认工具来做到这一点,该工具允许我放入一些图像。但我希望那里有一些“实时”的东西,例如显示应用程序加载状态的进度条、一些动态文本等。

如何做我做这个?要开始做这样的事情我需要知道哪些事情?

I want to create a splash screen for my Java application. I managed to do this using the NetBeans default tool that allows me to put some image in. But i want to have something "live" there, such as a progress bar showing the status of application load, some dynamic text, etc.

How do I do this? What are the things I need to know to start doing something like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

战皆罪 2024-11-17 09:54:38

这是 Java 教程,将引导您完成您想要做的事情。您可以在命令行上设置图像,以便它立即显示,然后您可以在 JVM 初始化后操作它以添加文本、进度条等。

http://download.oracle.com/javase/tutorial/uiswing/misc/splashscreen.html

Here is the Java tutorial walking you through exactly what you want to do. You can set the image on the command line so that it shows immediately, then you can manipulate it once the JVM is initialized to add text, progress bars, etc.

http://download.oracle.com/javase/tutorial/uiswing/misc/splashscreen.html

ゃ人海孤独症 2024-11-17 09:54:38

诀窍是使用 swing 创建启动屏幕,然后使用 Java 反射调用加载应用程序的方法(该方法位于另一个 .java 文件中)。加载完成后,处理您的启动屏幕。

检查代码后,您将了解它是如何工作的,现在可以按照自己的方式自定义它。

这是一些代码:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JDialog;

/**
 *
 * @author martijn
 */
public class Splash {

    public static void splash() {
        try {
            final BufferedImage img = ImageIO.read(Splash.class.getResourceAsStream("/path/to/your/splash/image/splash.png"));
            JDialog dialog = new JDialog() {

                @Override
                public void paint(Graphics g) {
                    g.drawImage(img, 0, 0, null);
                }
            };
            // use the same size as your image
            dialog.setPreferredSize(new Dimension(450, 300)); 
            dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
            dialog.setUndecorated(true);
            dialog.pack();
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
            dialog.repaint();
            try {
                // Now, we are going to init the look and feel:

                Class uim = Class.forName("javax.swing.UIManager");
                uim.getDeclaredMethod("setLookAndFeel", String.class).invoke(null, (String) uim.getDeclaredMethod("getSystemLookAndFeelClassName").invoke(null));

                // And now, we are going to invoke our loader method:
                Class clazz = Class.forName("yourpackage.YourClass");
                dialog.dispose();
                // suppose your method is called init and is static
                clazz.getDeclaredMethod("init").invoke(null);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            dialog.dispose();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

The trick is to create a splash screen using swing and then invoke using Java reflection the method, which is in another .java file, that loades the application. When done loading, dispose your splash screen.

After checking the code, you will understand how it works and now customize it your own way.

Here is some code:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JDialog;

/**
 *
 * @author martijn
 */
public class Splash {

    public static void splash() {
        try {
            final BufferedImage img = ImageIO.read(Splash.class.getResourceAsStream("/path/to/your/splash/image/splash.png"));
            JDialog dialog = new JDialog() {

                @Override
                public void paint(Graphics g) {
                    g.drawImage(img, 0, 0, null);
                }
            };
            // use the same size as your image
            dialog.setPreferredSize(new Dimension(450, 300)); 
            dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
            dialog.setUndecorated(true);
            dialog.pack();
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
            dialog.repaint();
            try {
                // Now, we are going to init the look and feel:

                Class uim = Class.forName("javax.swing.UIManager");
                uim.getDeclaredMethod("setLookAndFeel", String.class).invoke(null, (String) uim.getDeclaredMethod("getSystemLookAndFeelClassName").invoke(null));

                // And now, we are going to invoke our loader method:
                Class clazz = Class.forName("yourpackage.YourClass");
                dialog.dispose();
                // suppose your method is called init and is static
                clazz.getDeclaredMethod("init").invoke(null);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            dialog.dispose();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文