当通过 Jar 文件执行时启动画面不显示,但当从 Netbeans 执行时它工作 IDE
Splash screen not display when execute by Jar File but it works when execute from Netbeans IDE
从 Project properties -> Run Option
设置应用程序启动画面
StartApp绘制的SplashScreen Class
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();
}
}
}
以上代码是绘制启动画面。请说明为什么它在 jar file
中不起作用,而在 Netbeans IDE
中起作用。
命令行参数-splash:
表示磁盘上的图像文件,不能引用嵌入资源(你的图像已经变成了)。
你永远不应该使用任何包含 src
的路径引用,一旦程序是 built/packaged/exported.
它就不会存在
我建议改用清单方法,如 How to Create a Splash Screen 中标题为 "How to Use a JAR File to Display Splash Screen"
的部分所述
Netbeans 实际上有能力为您自动执行此操作,包括将图像打包到 Jar 中并为您更新清单文件
从 Project properties -> Run Option
StartApp绘制的SplashScreen Class
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();
}
}
}
以上代码是绘制启动画面。请说明为什么它在 jar file
中不起作用,而在 Netbeans IDE
中起作用。
命令行参数-splash:
表示磁盘上的图像文件,不能引用嵌入资源(你的图像已经变成了)。
你永远不应该使用任何包含 src
的路径引用,一旦程序是 built/packaged/exported.
我建议改用清单方法,如 How to Create a Splash Screen 中标题为 "How to Use a JAR File to Display Splash Screen"
的部分所述Netbeans 实际上有能力为您自动执行此操作,包括将图像打包到 Jar 中并为您更新清单文件