【发布时间】:2016-02-18 17:27:43
【问题描述】:
我找到了这段代码,我正在尝试将其调整到我的项目中。
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/ 文件夹,它是资源的第二个文件夹,但也尝试 root 不起作用。相反,它可以读取 .jar 文件中的两个 eclipse,每个图像作为参数传递,以调用在图像中显示错误消息的下一个类:
err.err("fail.jpg");
项目文件夹的格式如下:
项目:
- src(类)
- 来源(图片)
- 照片(照片)
所有路径名都是正确的,也就是“fail.jpg”。 在我寻找解决方案的几周内,我需要知道如何直接从资源文件夹上传图像以使它们直接在 .jar 文件中工作。非常感谢!
【问题讨论】:
标签: java image jar directory photos