Я сделал приложение Java, которое загружает файлы изображений из каталога, называемого «Images».javac Swing.java
java Swing
< /code>
Наоборот, если я скомпилируюсь и строю для создания файла JAR таким образом, он не может найти изображения в каталоге «Изображения».javac Swing.java
jar cfe Build.jar Swing .
java -jar Build.jar
< /code>
Я думаю, что это проблема пути в инструкциях Java.import javax.swing.*;
import java.net.URL;
class Swing extends JFrame {
private JLabel label = new JLabel();
//private String imageFilename = new String();
private JLabel image = new JLabel();
static String[] myStrings = {"String1", "String2", "String3"};
static String[] myImages = {"images\\image-1.jpg", "images\\image-2.jpg", "images\\image-3.jpg"};
Swing()
{
super("Swing Test Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1000, 700); //300
setLayout(null);
setLocationRelativeTo(null);
label.setBounds(10, 0, 220, 100);
add(label);
image.setBounds(10, 200, 420, 420);
add(image);
setVisible(true);
}
void SetText(String strLabel, String imageFilename)
{
label.setText(strLabel);
URL imageURL = getClass().getResource(imageFilename);
if (imageURL != null) {
ImageIcon imageIcon = new ImageIcon(imageURL);
image.setIcon(imageIcon);
} else {
System.err.println("Image " + imageFilename + " not found.");
}
}
public static void main(String arg[])
{
Swing swing = new Swing();
int i = 0;
while (true)
{
swing.SetText(myStrings, myImages);
i++;
if (i == 3)
i = 0;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
< /code>
С файлом jar я получаю сообщение: «Изображение не найдено.» Если я компилируюсь с Javac Swing.java плюс java swing , я могу получить доступ к приведенному выше каталогу.private JLabel image = new JLabel();
//...
URL imageURL = getClass().getResource(imageFilename);
ImageIcon imageIcon = new ImageIcon(imageURL);
image.setIcon(imageIcon);
Подробнее здесь: https://stackoverflow.com/questions/796 ... -directory