gpt4 book ai didi

java - 通过 Jar 文件执行时不显示启动画面,但在从 Netbeans IDE 执行时它可以工作

转载 作者:行者123 更新时间:2023-11-30 08:11:40 25 4
gpt4 key购买 nike

Project properties -> Run Option 设置应用程序启动画面

enter image description here

StartApp类绘制的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();
}
}
}

以上代码是绘制启动画面。请说明为什么它在 jar 文件 中不能工作,而在 Netbeans IDE 中工作。

最佳答案

命令行参数-splash:表示磁盘上的一个图像文件,不能引用嵌入资源(你的图像已经变成了)。

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

我建议改用 list 方法,如 How to Create a Splash Screen 中所述,在标题为“如何使用 JAR 文件显示启动画面”的部分

Netbeans 实际上有能力自动为您做这件事,包括将图像打包到 Jar 中并为您更新 list 文件

Splash Screen Properties

关于java - 通过 Jar 文件执行时不显示启动画面,但在从 Netbeans IDE 执行时它可以工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30859634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com