将带有图像的JLabel添加到JList以显示所有图像 [英] Add JLabel with image to JList to show all the images

查看:146
本文介绍了将带有图像的JLabel添加到JList以显示所有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码。它不会在框架中显示图像,而是显示一些文本。有人请建议我,我应该在代码中做出什么改变,以便它允许我在一个框架中显示图像?

Here is my code. It does not show images in the frame and instead shows some text. would anybody please suggest me that what change I should make in the code so that it allows me to show the images in a frame?

import java.awt.Component;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;

public class ListView {


public static void main(String[] args) throws IOException {
    JFrame frame=new JFrame();
    frame.setSize(500,500);
    JLabel lbl[] = new JLabel[10];
    DefaultListModel listModel;
     ImageIcon[] b = new   ImageIcon[10];
    //JList lsm=new JList();
    listModel = new DefaultListModel();
     File folder = new File("C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures");
     File[] listOfFiles = folder.listFiles();
      JLabel[] lb=new JLabel[15];
    for (int i = 0; i < listOfFiles.length; i++) 
    {
          System.out.println("chek panth"+listOfFiles[i].getName().toString());
  //      b[i] = ImageIO.read(new File("C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/" + listOfFiles[i].getName().toString()));
         b[i] = new ImageIcon("C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/" + listOfFiles[i].getName().toString());
         lb[i]=new JLabel(b[i]);
         listModel.add(i, lb[i]);

    }
    JList lsm=new JList(listModel);

    Component add = frame.add(new JScrollPane(lsm));

    frame.setVisible(true);

}


}


推荐答案

请注意,我不会以这种方式设计代码,但我希望将其保持尽可能接近原始状态,同时使其能够在基于Windows的图像上显示图像列表框。

Note that I would not design the code this way, but I wanted to keep it as close to the original as practical, while making it work to display a list of images on a Windows based box.

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ListView {

    public static void main(String[] args) throws IOException {
        String path = "C:/Documents and Settings/All Users/Documents/" +
            "My Pictures/Sample Pictures";
        JFrame frame=new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
        DefaultListModel listModel = new DefaultListModel();
        int count = 0;
        for (int i = 0; i < listOfFiles.length; i++)
        {
            System.out.println("check path"+listOfFiles[i]);
            String name = listOfFiles[i].toString();
            // load only JPEGs
            if ( name.endsWith("jpg") ) {
                ImageIcon ii = new ImageIcon(ImageIO.read(listOfFiles[i]));
                listModel.add(count++, ii);
            }
        }
        JList lsm=new JList(listModel);
        lsm.setVisibleRowCount(1);

        frame.add(new JScrollPane(lsm));

        frame.pack();
        frame.setVisible(true);
    }
}

这篇关于将带有图像的JLabel添加到JList以显示所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆