display image in Java

2020-07-24 06:44发布

问题:

I am working on a desktop application for windows version using java. In my application there is a requirement to display IMAGES from Path with a next and previous button.

For that I wrote a class to return all the paths of the images : I use ArrayList

import java.io.File;
import java.util.ArrayList;


public class RE {
    private ArrayList<String> c =new ArrayList<String>();
public RE (String rep)
{
    File src=new File(rep);
    if(src!=null && src.exists() && src.isDirectory())
    {
        String[] tab=src.list();
        if(tab!=null)
        {
        for(String s:tab)
        {
            File srcc=new File(rep+File.separatorChar+s);
            if(srcc.isFile())
            {  
                if(srcc.getName().matches(".*"+"png$")|| srcc.getName().matches(".*"+"jpg$") || srcc.getName().matches(".*"+"gif$"))
                c.add(srcc.getPath());
            }

        }
        }
    }
}

public ArrayList<String> getAll()
{
    return c;


}
}

and a class to display images, but I have some problems in ActionPerformed

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ListIterator;

import javax.swing.*;


public class swing extends JFrame implements ActionListener{
    private RE c=new RE("H:\\photos\\g");
    JTextField chemin=new JTextField(30);
    JLabel lab;ImageIcon imageIcon;
    JButton next =new JButton("NEXT");
    JButton prev=new JButton("prev");
    JPanel pan1=new JPanel();
    JPanel pan2=new JPanel();
    JPanel pan3=new JPanel();
     swing()
     {
         imageIcon = new ImageIcon(c.getAll().get(2));
         lab = new JLabel(imageIcon);
         this.setLayout(new BorderLayout());
         this.setVisible(true);
         pan1.setLayout(new FlowLayout());
         pan1.add(new JLabel("ENTREZ LE CHEMIN DE REPERTOIRE :"));
         pan1.add(chemin);

         pan2.setLayout(new FlowLayout());
         pan2.add(lab);


         pan3.setLayout(new FlowLayout());
         next.addActionListener(this);
         prev.addActionListener(this);
         pan3.add(prev); pan3.add(next);


         this.add(pan1,BorderLayout.NORTH);
         this.add(pan2,BorderLayout.CENTER);
         this.add(pan3,BorderLayout.SOUTH);
         this.pack();
     }


     public static void main(String[] args){
         new swing();
     }


    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==next)
        {
        String cur=imageIcon.toString();
        ListIterator<String> l=c.getAll().listIterator(c.getAll().indexOf(cur));
        lab.setIcon(new ImageIcon(l.previous().toString()));
        }
        else
        {

        }

    }

}

but I can't complete that :

public void actionPerformed(ActionEvent e) {
        if(e.getSource()==next)
        {
        String cur=imageIcon.toString();
        ListIterator<String> l=c.getAll().listIterator(c.getAll().indexOf(cur));
        lab.setIcon(new ImageIcon(l.previous().toString()));
        }
        else
        {

        }

    }

回答1:

Use an appropriate layout manager. In this case, use CardLayout. This will make image swapping easier. Unless the number of images is absurdly large, then I highly recommend this approach.



回答2:

Use your List<String> to construct a corresponding List<ImageIcon> and replace the label's icon as required. In this example, a JComboBox holds the current selection, and the buttons change the selection accordingly. Note that the indexes wrap around, forming a circular queue.