如何使用 JFileChoose 加载图像文件夹并添加下一个,上一个按钮以在 JLabel 中一张一张地预览图像?

How to load image folder with JFileChoose and add next ,previous button to preview image one by one in JLabel?

我正在使用 Jlabel 设置 JFrame 以查看其中的图像和这张图像我从一个文件夹上传它并且我添加了下一个和上一个按钮以在使用 Jfilechooser 按钮选择文件夹后在该标签中获取下一个和上一个图像视图。 在我加载文件夹并单击要查看的第一张图像然后我按下一个按钮继续同时加载之后我没有在 Jlabel 中得到任何图像它是一个没有图像的黑屏..它只显示我点击的第一张图像在其中,下一个按钮无法获取下一个

第一种从JFileChooser获取图片的方法

private String[] getImages() {
    imgch = new JFileChooser(".");
    // File file = new File(getClass().getResource("/").getFile());
    File file = imgch.getCurrentDirectory();
    String[] imagesList = file.list();            
    return imagesList;     
  } 

第二:显示图片

private void showImage(int index) throws IOException {
    String[] imagesList = getImages();
    String imageName = imagesList[index];
    File file = imgch.getSelectedFile();
    ImageIcon icon = new ImageIcon(file+imagesList);
          Image newImg = 
   icon.getImage().getScaledInstance(canvas.getWidth(), 
    canvas.getHeight(), Image.SCALE_SMOOTH);

    viewer.setIcon(new ImageIcon(newImg));
}

通过更改 showImage() 中的这一行解决 File file = imgch.getSelectedFile();
File file = imgch.getCurrentDirectory(); path = file.getAbsolutePath().concat("/"+imageName); 然后将“路径”传递给 ImageIcon