Splashscreen在背景中运行

时间:2015-03-03 09:21:18

标签: java splash-screen

我的SplashScreen出现问题但没有显示,但在后台运行..

继承我的代码,取自this教程(希望可以发布链接?!?):

SplashScreen类:

public final class SplashScreen extends JWindow {


BorderLayout borderLayout1 = new BorderLayout();
  JLabel imageLabel = new JLabel();
  JPanel southPanel = new JPanel();
  FlowLayout southPanelFlowLayout = new FlowLayout();
  JProgressBar progressBar = new JProgressBar();
  ImageIcon imageIcon;

  public SplashScreen(ImageIcon imageIcon) {
    this.imageIcon = imageIcon;
    try {
      jbInit();
    }
    catch(Exception ex) {
    }
  }

  // note - this class created with JBuilder
  void jbInit() throws Exception {
    imageLabel.setIcon(imageIcon);
    this.getContentPane().setLayout(borderLayout1);
    southPanel.setLayout(southPanelFlowLayout);
    southPanel.setBackground(Color.BLACK);
    this.getContentPane().add(imageLabel, BorderLayout.CENTER);
    this.getContentPane().add(southPanel, BorderLayout.SOUTH);
    southPanel.add(progressBar, null);
    this.pack();
  }

  public void setProgressMax(int maxProgress)
  {
    progressBar.setMaximum(maxProgress);
  }

  public void setProgress(int progress)
  {
    final int theProgress = progress;
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        progressBar.setValue(theProgress);
      }
    });
  }

  public void setProgress(String message, int progress)
  {
    final int theProgress = progress;
    final String theMessage = message;
    setProgress(progress);
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        progressBar.setValue(theProgress);
        setMessage(theMessage);
      }
    });
  }

  public void setScreenVisible(boolean b)
  {
    final boolean boo = b;
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        setVisible(boo);
      }
    });
  }

  private void setMessage(String message)
  {
    if (message==null)
    {
      message = "";
      progressBar.setStringPainted(false);
    }
    else
    {
      progressBar.setStringPainted(true);
    }
    progressBar.setString(message);
  }
}

在我的Main()中,我像这样启动SplashScren:

SplashScreen initscreen;
ImageIcon myImage = new ImageIcon(FilterConfiguratorJFrame.class.getResource("/images/logo.png"));
initscreen = new SplashScreen(myImage);
initscreen.setLocationRelativeTo(null);
initscreen.setProgressMax(100);
initscreen.setScreenVisible(true);

然后我设置了这样的进度:

initscreen.setProgress("...loading Projects" , 40);

然后我的程序正在通过VPN从工作中的服务器加载项目。

直到这里我没有收到任何错误,但SplashScreen并没有出现......

我有一个OptionPane出现,当没有连接到服务器时,以及当这个OptionPane显示SplashScreen时。当我点击" OK"它在OptionPane上消失了,SplashScreen也消失了......

我尝试了,无论我何时何地让OptionPane显示,SplashScreen都会出现......

所以我的问题是,如何强制SplashScreen进入前台?

我希望SplashScreen在启动时显示并更新其进度条,同时从数据库加载不同的数据(可以持续1-2分钟)。

编辑:试图强制SplashScreen通过showinf显示一个JFrame,没有'工作,似乎只能使用OptionPanes。

0 个答案:

没有答案