我遇到了filechooser的问题,将目录路径完全带入了图像查看器。我想从所选目录中获取所有图像。我在行
中得到了NullPointerExceptionString[] imagesList = selectedDirectory.list();
以下是代码:
JFileChooser wybierz = new JFileChooser();
File selectedDirectory;
String path;
int pos = 0;
public String[] getImages() {
String[] imagesList = selectedDirectory.list();
return imagesList;
}
public void showImage(int index) {
String[] imagesList = getImages();
String imageName = imagesList[index];
ImageIcon icon = new ImageIcon(getClass().getResource(path));
Image image = icon.getImage().getScaledInstance(jLabel_Image.getWidth(), jLabel_Image.getHeight(), Image.SCALE_SMOOTH);
jLabel_Image.setIcon(new ImageIcon(image));
}
public void fileChooser() {
wybierz.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
wybierz.setAcceptAllFileFilterUsed(false);
wybierz.addChoosableFileFilter(new FileNameExtensionFilter("Image Files", "jpg", "png", "tif"));
int v = wybierz.showOpenDialog(null);
if (v == JFileChooser.APPROVE_OPTION) {
File selectedDirectory = wybierz.getSelectedFile();
showImage(pos);
}
}
问题是什么?