【问题标题】:best example for programmatically creating SplashScreen with text以编程方式创建带有文本的 SplashScreen 的最佳示例
【发布时间】:2013-04-23 16:32:33
【问题描述】:

我需要以编程方式创建一个 SplashScreen 并向其中添加文本(并更改它)。 大多数示例都使用命令行参数。 有没有解决方案?

【问题讨论】:

    标签: java swing awt java-7 splash-screen


    【解决方案1】:

    SwingWorker 中加载内容时,您可以使用带有背景图像和进度条的未装饰对话框。完成后,隐藏对话框并照常启动 UI。添加到对话框/闪屏的组件必须是不透明的,才能“看到”背景图像。

    这是一个工作示例:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Frame;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.List;
    
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JProgressBar;
    import javax.swing.SwingUtilities;
    import javax.swing.SwingWorker;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestSplashScreen {
    
        private JDialog dialog;
        private JFrame frame;
        private JProgressBar progress;
    
        protected void initUI() throws MalformedURLException {
            showSplashScreen();
            SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {
    
                @Override
                protected Void doInBackground() throws Exception {
                    for (int i = 0; i < 100; i++) {
                        Thread.sleep(100);// Simulate loading
                        publish(i);// Notify progress
                    }
                    return null;
                }
    
                @Override
                protected void process(List<Integer> chunks) {
                    progress.setValue(chunks.get(chunks.size() - 1));
                }
    
                @Override
                protected void done() {
                    showFrame();
                    hideSplashScreen();
                }
    
            };
            worker.execute();
        }
    
        protected void hideSplashScreen() {
            dialog.setVisible(false);
            dialog.dispose();
        }
    
        protected void showSplashScreen() throws MalformedURLException {
            dialog = new JDialog((Frame) null);
            dialog.setModal(false);
            dialog.setUndecorated(true);
            JLabel background = new JLabel(new ImageIcon(new URL("http://blogs.dirteam.com/photos/sanderberkouwer/images/2157/original.aspx")));
            background.setLayout(new BorderLayout());
            dialog.add(background);
            JLabel text = new JLabel("Loading, please wait...");
            text.setForeground(Color.WHITE);
            text.setBorder(BorderFactory.createEmptyBorder(100, 50, 100, 50));
            background.add(text);
            progress = new JProgressBar();
            background.add(progress, BorderLayout.SOUTH);
            dialog.pack();
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
        }
    
        protected void showFrame() {
            frame = new JFrame(TestSplashScreen.class.getSimpleName());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JLabel ui = new JLabel("UI loaded and ready");
            ui.setBorder(BorderFactory.createEmptyBorder(300, 300, 300, 300));
            frame.add(ui);
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
                UnsupportedLookAndFeelException {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        new TestSplashScreen().initUI();
                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    

    【讨论】:

    • 太好了,谢谢。所以一个未装饰的对话框是自定义启动画面的唯一选择?
    • @DanielRuf 您也可以使用JWindow 代替JDialog,但它看起来是一样的(它会为您节省2 行代码)。 还有其他选择吗?也许吧,但到目前为止我想出了上面描述的那个,看起来还不错。
    【解决方案2】:

    另一种方法是使用SplashScreen API。详情请见How to Create a Splash Screen

    我需要以编程方式创建一个 SplashScreen 并向其中添加文本(并更改它)。

    致电SplashScreen.createGraphics() 并根据需要进行绘画。

    【讨论】:

    • 标记为解决方案的答案更适用于 JDialog。但两者都有效;-) 您推荐的解决方案还需要将启动画面设置为控制台参数或清单文件(看起来如此)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 2011-10-06
    • 2011-12-22
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多