图片显示问题
import java.awt.event.*;import javax.swing.*;
import javax.swing.event.*;
public class ImageViewer extends JFrame implements ListSelectionListener
{
private String FILE[] = {"","",""};
private DefaultListModel data = new DefaultListModel();
private JList list = new JList(data);
private JLabel image = null;
private class windowCloser extends WindowAdapter
{
public void windowClosing()
{
System.exit(0);
}
}
public ImageViewer()
{
super("Image Viewer");
for(int i=0;i<FILE.length;i++)
data.addElement("Image/"+FILE[i]);
image = new JLabel(new ImageIcon(FILE[0]),SwingConstants.CENTER);
list.setSelectedIndex(0);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollList = new JScrollPane(list);
JScrollPane scrollImage = new JScrollPane(image);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,scrollList,scrollImage);
getContentPane().add("Center",splitPane);
list.addListSelectionListener(this);
addWindowListener(new windowCloser());
validate();
pack();
setVisible(true);
}
public static void main(String[] args)
{
// TODO: Add your code here
ImageViewer iv = new ImageViewer();
String[] data = {"one", "two", "three", "four"};
JList dataList = new JList(data);
dataList.setSelectedIndex(1); // select "two"
dataList.getSelectedValue(); // returns "two"
}
public void valueChanged(ListSelectionEvent lse)
{
if(!lse.getValueIsAdjusting())
image.setIcon(new ImageIcon(list.getSelectedValue().toString()));
}
}
程序可以运行,但是在显示图片时只能显示第一个图片,点击其他的都不能显示,为什么啊?