[英]Trouble exporting to runnable jar in java eclipse project with images
我在将第一个项目导出到可执行jar文件时遇到麻烦。 它在eclipse中运行得很好,但是在导出后双击可执行jar时,它什么也没做。 我创建了多个测试程序并搜索了互联网以寻求帮助,并得出结论认为,这与程序中的图像有关。 我已经包含了测试程序中的所有代码。 它不会让我在项目中发布图像文件位置的图片,但是它位于源文件夹中。
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class test {
private JFrame frame;
JLabel label;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test window = new test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel("");
Image img = new ImageIcon(this.getClass().getResource("/YahtzeeBoard.png")).getImage();
label.setIcon(new ImageIcon(img));
label.setBounds(100, 100, 450, 300);
frame.getContentPane().add(label);
}
}
例如,制作一个像Icons之类的Java程序包,将图像放在其中,然后调用
getResource("Icons/YahtzeeBoard.png")
这样,图像将被包装在罐子中。
或者您可以将图像与jar放在相同的文件夹中,以找到路径“ /YahtzeeBoard.png”,因为它指向jar所在的文件夹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.