[英]load resource directory in jar file
我找到了這段代碼,我正在嘗試使其適應我的項目。
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URL;
public class Album {
static boolean contr = false;
static String path = "photos/";
static int MAX;
static String []imgName;
static JLabel label = new JLabel();
static JMenuBar mBar = new JMenuBar();
static JMenu mn = new JMenu("open here ");
static JMenuItem []menuItem;
static JFrame fot = new JFrame();
static JButton back = new JButton(" back ");
public static void Album() {
if (contr==true){
fot.setVisible(true);
}
else{
JFrame.setDefaultLookAndFeelDecorated(true);
File directory = new File(path);
if (!directory.exists()) {
System.err.println("The specified directory doesn't exist!!");
err.err("fail.jpg");
}
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
// Do nothing
}
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setVerticalAlignment(SwingConstants.CENTER);
mBar.updateUI();
mn.updateUI();
// Filter out the Images
imgName = directory.list(new FilenameFilter() {
String[] readFormat = ImageIO.getReaderFormatNames();
@Override
public boolean accept(File dir, String name) {
for (int i = 0; i < readFormat.length; i++) {
if (name.endsWith(readFormat[i])) {
return true;
}
}
return false;
}
});
MAX = imgName.length;
if (MAX == 0) {
System.err.println("OOps , no images");
System.exit(-1);
}
//Limit the maximunm entries to 10
if (MAX > 19) {
MAX = 19;
}
menuItem = new JMenuItem[MAX];
for (int i = 0; i < menuItem.length; i++) {
menuItem[i] = new JMenuItem(imgName[i].substring(0, imgName[i].lastIndexOf(".")), new ImageIcon(getImage(path + imgName[i]).getScaledInstance(32, 32, Image.SCALE_SMOOTH)));
menuItem[i].updateUI();
mn.add(menuItem[i]);
menuItem[i].setActionCommand(imgName[i]);
menuItem[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
label.setIcon(new ImageIcon(getImage(path + ae.getActionCommand())));
}
});
}
mBar.add(mn);
fot.setJMenuBar(mBar);
fot.add(new JScrollPane(label));
fot.add(back, BorderLayout.AFTER_LAST_LINE);
Dimension scrDim = Toolkit.getDefaultToolkit().getScreenSize();
fot.setSize(scrDim.width-250 , scrDim.height-50);
fot.setVisible(true);
fot.setTitle("photo album");
fot.setResizable(false);
fot.setLocationRelativeTo(null);
fot.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
back.addActionListener(new ActionListener() { //back
@Override
public void actionPerformed(ActionEvent e) {
fot.setVisible(false);
contr=true;
menu.menu();
}
});
}
}
// Get the Image from the disk
public static Image getImage(String fileName) {
try {
return ImageIO.read(new File(fileName));
} catch (IOException ioe) {
System.err.println("Error loading Image : " + fileName);
}
return null;
}
}
我必須將所有文件導出為.jar文件,從eclipse正常運行,而一旦導出的.jar文件則無法工作。 問題是我找不到photos /文件夾,它是資源的第二個文件夾,但是嘗試到根目錄卻無法正常工作。 相反,它可以讀取.jar文件中的兩個eclipse(作為參數傳遞的每個圖像),就像調用下一個在圖像中顯示錯誤消息的類一樣:
err.err("fail.jpg");
項目文件夾的模式如下:
項目:
所有路徑名均正確,為“ fail.jpg”。 我正在尋找解決方案的星期已經過去了,我需要知道如何直接從資源文件夾中上傳圖像,以使其直接在.jar文件中工作。 非常感謝!
您應該考慮使用getResourceAsStream("/fail.jpg")
方法來獲取文件。 一個例子看起來像
InputStream inputStream = Main.class.getResourceAsStream("/fail.jpg");
對於此示例,您的jpg文件應位於您的源文件夾中。
您可以在jar文件中使用任何類來代替Main。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.