由Jar File执行时不显示启动画面,但从Netbeans IDE执行时它可以正常工作

时间:2015-06-16 05:43:06

标签: java netbeans jar

Project properties -> Run Option

设置应用程序启动画面

enter image description here

由StartApp Class绘制的SplashScreen

   public class StartApp {

public static void main(String[] args) {
    new Thread(new Runnable() {

        public void run() {
            splashInit();           // initialize splash overlay drawing parameters
            appInit();              // simulate what an application would do before starting
            if (mySplash != null) // check if we really had a spash screen
            {
                mySplash.close();   // we're done with it
            }
        }
    }).start();

    // begin with the interactive portion of the program
    Context.initialize();


}
static SplashScreen mySplash;                   // instantiated by JVM we use it to get graphics
static Graphics2D splashGraphics;               // graphics context for overlay of the splash image
static Rectangle2D.Double splashTextArea;       // area where we draw the text
static Rectangle2D.Double splashProgressArea;   // area where we draw the progress bar
static Font font;                               // used to draw our text

/**
 * just a stub to simulate a long initialization task that updates
 * the text and progress parts of the status in the Splash
 */
private static void appInit() {
    Random random = new Random();
    int progress = 0;
    splashProgress(0);
    while (progress < 100) {
        //Sleep for up to one second.
        splashText("Please wait... Application is starting " + progress + "%");
        try {
            Thread.sleep(random.nextInt(1000));
        } catch (InterruptedException ex) {
            break;
        }
        //Make random progress.
        progress += random.nextInt(20);
        splashProgress(Math.min(progress, 100));
    }
}

/**
 * Prepare the global variables for the other splash functions
 */
private static void splashInit() {
    // the splash screen object is created by the JVM, if it is displaying a splash image

    mySplash = SplashScreen.getSplashScreen();
    // if there are any problems displaying the splash image
    // the call to getSplashScreen will returned null

    if (mySplash != null) {
        // get the size of the image now being displayed
        Dimension ssDim = mySplash.getSize();
        int height = ssDim.height;
        int width = ssDim.width;

        // stake out some area for our status information
        splashTextArea = new Rectangle2D.Double(20, height * 0.91, width * .50, 25.);
        splashProgressArea = new Rectangle2D.Double(1.0, height * .87, width - 2, 3);

        // create the Graphics environment for drawing status info
        splashGraphics = mySplash.createGraphics();
        font = new Font("Dialog", Font.PLAIN, 14);
        splashGraphics.setFont(font);

        // initialize the status info
        splashText("Starting");
        splashProgress(0);
    }
}

/**
 * Display text in status area of Splash.  Note: no validation it will fit.
 * @param str - text to be displayed
 */
public static void splashText(String str) {
    if (mySplash != null && mySplash.isVisible()) {   // important to check here so no other methods need to know if there
        // really is a Splash being displayed

        // erase the last status text
        splashGraphics.setPaint(new Color(248, 249, 250));
        splashGraphics.fill(splashTextArea);

        // draw the text
        splashGraphics.setPaint(Color.BLACK);
        splashGraphics.drawString(str, (int) (splashTextArea.getX() + 10), (int) (splashTextArea.getY() + 15));

        // make sure it's displayed
        mySplash.update();
    }
}

/**
 * Display a (very) basic progress bar
 * @param pct how much of the progress bar to display 0-100
 */
public static void splashProgress(int pct) {
    if (mySplash != null && mySplash.isVisible()) {

        // Note: 3 colors are used here to demonstrate steps
        // erase the old one
        splashGraphics.setPaint(new Color(230, 230, 230));
        splashGraphics.fill(splashProgressArea);

        // draw an outline
        // Calculate the width corresponding to the correct percentage
        int x = (int) splashProgressArea.getMinX();
        int y = (int) splashProgressArea.getMinY();
        int wid = (int) splashProgressArea.getWidth();
        int hgt = (int) splashProgressArea.getHeight();

        int doneWidth = Math.round(pct * wid / 100.f);
        doneWidth = Math.max(0, Math.min(doneWidth, wid - 1));  // limit 0-width

        // fill the done part one pixel smaller than the outline
        splashGraphics.setPaint(new Color(21, 106, 151));
        splashGraphics.fillRect(x, y - 2, doneWidth, hgt + 1);

        // make sure it's displayed
        mySplash.update();
    }
 }
 }

以上代码是绘制Splash Screen。请在jar file中使用Netbeans IDE时告诉我<UIGestureRecognizerDelegate>无效的原因。

1 个答案:

答案 0 :(得分:3)

命令行参数-splash:表示磁盘上的图像文件,您无法引用嵌入的资源(图像已经成为)。

你不应该使用包含src的任何路径引用,一旦构建/打包/导出程序,它就不会存在。

我建议使用清单方法,如How to Create a Splash Screen中所述,标题为“如何使用JAR文件显示启动画面”一节

Netbeans实际上有能力自动为您执行此操作,包括将图像打包到Jar中并更新

的清单文件

Splash Screen Properties