Java 当通过Jar文件执行时,启动屏幕不显示,但当从Netbeans IDE执行时,启动屏幕工作
从Java 当通过Jar文件执行时,启动屏幕不显示,但当从Netbeans IDE执行时,启动屏幕工作,java,netbeans,jar,Java,Netbeans,Jar,从项目属性->运行选项设置应用程序启动屏幕 由StartApp Class绘制的飞溅屏幕 public class StartApp { public static void main(String[] args) { new Thread(new Runnable() { public void run() { splashInit(); // initialize splash overlay drawing p
项目属性->运行选项设置应用程序启动屏幕
由StartApp 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();
}
}
}
公共类StartApp{
公共静态void main(字符串[]args){
新线程(newrunnable()){
公开募捐{
splashInit();//初始化飞溅覆盖图形参数
appInit();//模拟应用程序在启动前的操作
if(mySplash!=null)//检查我们是否真的有一个spash屏幕
{
mySplash.close();//我们已经完成了
}
}
}).start();
//从节目的互动部分开始
Context.initialize();
}
静态SplashScreen mySplash;//由JVM实例化,我们使用它来获取图形
静态图形2D splashGraphics;//用于覆盖飞溅图像的图形上下文
静态矩形2D.Double splashTextArea;//我们绘制文本的区域
静态矩形2D.Double splashProgressArea;//绘制进度条的区域
静态字体;//用于绘制文本
/**
*只是一个存根来模拟一个长的初始化任务
*Splash中状态的文本和进度部分
*/
私有静态void appInit(){
随机=新随机();
int progress=0;
进展(0);
而(进度<100){
//最多睡一秒钟。
splashText(“请稍候…应用程序正在启动”+进度+“%”);
试一试{
Thread.sleep(random.nextInt(1000));
}捕获(中断异常例外){
打破
}
//随机取得进展。
进度+=随机。下一步(20);
进度(数学分钟(进度,100));
}
}
/**
*为其他splash函数准备全局变量
*/
私有静态void splashInit(){
//如果启动屏幕对象显示启动图像,则它由JVM创建
mySplash=SplashScreen.getSplashScreen();
//如果显示飞溅图像时出现任何问题
//对getSplashScreen的调用将返回null
如果(mySplash!=null){
//获取当前显示的图像的大小
维度ssDim=mySplash.getSize();
int height=ssDim.height;
int width=ssDim.width;
//为我们的状态信息标出一些区域
splashTextArea=新矩形2D.Double(20,高度*0.91,宽度*0.50,25);
splashProgressArea=新矩形2D.Double(1.0,高度*.87,宽度-2,3);
//为图形状态信息创建图形环境
splashGraphics=mySplash.createGraphics();
font=新字体(“对话框”,font.PLAIN,14);
splashGraphics.setFont(字体);
//初始化状态信息
文本(“开始”);
进展(0);
}
}
/**
*在Splash的状态区域中显示文本。注意:没有验证它是否合适。
*@param str-要显示的文本
*/
公共静态文本(字符串str){
if(mySplash!=null&&mySplash.isVisible()){//此处需要检查,因此没有其他方法需要知道是否存在
//真的是一个飞溅正在显示
//删除最后一个状态文本
splashGraphics.setPaint(新颜色(248249250));
splashGraphics.fill(splashTextArea);
//画课文
splashGraphics.setPaint(颜色:黑色);
绘图字符串(str,(int)(splashTextArea.getX()+10),(int)(splashTextArea.getY()+15));
//确保它被显示出来
mySplash.update();
}
}
/**
*显示(非常)基本的进度条
*@param pct进度条显示多少0-100
*/
公共静态进度(内部pct){
if(mySplash!=null&&mySplash.isVisible()){
//注:此处使用3种颜色来演示步骤
//把旧的擦掉
splashGraphics.setPaint(新颜色(230、230、230));
飞溅图形。填充(飞溅区域);
//勾勒
//计算与正确百分比对应的宽度
intx=(int)splashProgressArea.getMinX();
int y=(int)progressArea.getMinY();
int wid=(int)splashProgressArea.getWidth();
int hgt=(int)progressArea.getHeight();
int doneWidth=数学圆(pct*wid/100.f);
doneWidth=Math.max(0,Math.min(doneWidth,wid-1));//限制0-width
//将完成部分填充为比轮廓小一个像素
splashGraphics.setPaint(新颜色(21106151));
splashGraphics.fillRect(x,y-2,doneWidth,hgt+1);
//确保它被显示出来
mySplash.update();
}
}
}
上面的代码是用来画闪屏的。请说明为什么它不能在jar文件中工作
而在Netbeans IDE中工作
命令行参数-splash:
表示磁盘上的映像文件,您不能引用嵌入的资源(映像已成为)
您不应该使用任何包含src
的路径引用,一旦程序构建/打包/导出,它就不存在了
我建议改为使用清单方法,如“如何使用JAR文件显示初始屏幕”一节中所述
Netbeans实际上能够自动为您完成这项工作,包括将映像打包到Jar中,并为您的应用程序更新清单文件
我怀疑图像路径在JAR中时会发生变化。您能解压jar并显示其内容(或至少显示图像路径)吗?-splash
希望图像文件位于您的应用程序外部,因此它不能位于您的src
目录中,在构建/打包应用程序时它将不存在。查看更多详细信息hanks@MadProgram