best example for programmatically creating SplashScreen with text

好久不见. 提交于 2019-11-27 04:54:10

问题


I need to create a SplashScreen programmatically and add text to it (and change it). Most examples work with thecommand line parameters. Are there solutions working without?


回答1:


You can use an undecorated dialog with a background image and a progress bar while loading stuffs in a SwingWorker. When done, hide the dialog and start the UI as usual. Components added to the dialog/splashcreen must be non-opaque in order to "see" the background image.

Here is a working example:

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



回答2:


An alternative is to use the SplashScreen API. See How to Create a Splash Screen for details.

I need to create a SplashScreen programmatically and add text to it (and change it).

Call SplashScreen.createGraphics() and paint as needed.



来源:https://stackoverflow.com/questions/16288303/best-example-for-programmatically-creating-splashscreen-with-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!