我正在尝试将JComponent函数作为库。它必须能够在其上显示保存的图像,这些图像将在生活在JScrollPane中的组件中显示。我还将添加删除图像的功能。我设法在组件上显示图像。我该怎么做呢?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
public class GalleryPanel extends JPanel
{
private static final long serialVersionUID = 1L;
private int currentImage;
private JLabel[] images;
private final int MAX_IMAGES = 12;
private JScrollPane scrollPane;
private JPanel imageGallery;
public void init()
{
setLayout(new BorderLayout());
images = new JLabel[MAX_IMAGES];
imageGallery = new JPanel();
imageGallery.setLayout(new GridLayout(12,1,10,10));
scrollPane = new JScrollPane();
scrollPane.setBackground(Color.RED);
scrollPane.add(imageGallery);
add(scrollPane, BorderLayout.CENTER);
setBackground(Color.GRAY);
}
public void addImageToGallery(File file)
{
if ( currentImage <= images.length - 1)
{
BufferedImage bufImage = null;
try
{
bufImage = ImageIO.read(file); //tries to load the image
}
catch (Exception e)
{
System.out.println("Unable to load file " + file.toString());
}
Image resizedImage = bufImage.getScaledInstance(bufImage.getWidth()/6, bufImage.getHeight()/6, Image.SCALE_SMOOTH);
ImageIcon icon = new ImageIcon(resizedImage);
images[currentImage] = new JLabel(icon, JLabel.CENTER);
scrollPane.add(images[currentImage]);
images[currentImage].setSize(120, 100);
scrollPane.add(images[currentImage]);
currentImage++;
//scrollPane.revalidate();
//scrollPane.repaint();
}
else
{
throw new ArrayIndexOutOfBoundsException("The gallery is full");
}
}
public final int getMaxImages()
{
return MAX_IMAGES;
}
public Dimension getPreferredSize()
{
return new Dimension(300, 700);
}
}
答案 0 :(得分:0)
scrollPane.add(images[currentImage]);
images[currentImage].setSize(120, 100);
scrollPane.add(images[currentImage]);
不要将组件直接添加到滚动窗格。
JScrollPane
使用JViewport
来保存要显示的组件。
因此,如果您想使用JList
,请使用:
JList<Icon> list = new JList<Icon>();
JScrollPane scrollPane = new JScrollPane( list );
frame.add( scrollPane );
然后将Icon
(不是JLabel)添加到JList
。 JList
已有自定义渲染器显示Icon
。
阅读How to Use Lists上Swing教程中的部分,了解更多信息和工作示例。
本教程还有一个关于How to Use Scroll Panes
的部分,你应该阅读。
答案 1 :(得分:0)
我使用JImagePanel
,JPanel
,JList
和JLabel
ListCellRenderer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javax.swing;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.Vector;
import javax.swing.border.LineBorder;
/**
*
* @author NiRRaNjAN RauT
*/
/**
* JImagePanel is used to display JList of BufferedImage
* This class is extended to JPanel
*/
public class JImagePanel extends JPanel {
/**
* JList is used to display display BufferedImage
*/
private JList imageList = null;
/**
* JScrollPane is used to add scroll bar to JList
*/
JScrollPane scroll_pane = null;
/**
* Vector<BufferedImage> is used to store the BufferedImages
*/
Vector<BufferedImage> listData = new Vector<BufferedImage>();
/**
* default constructor used to initialize JPanel
*/
public JImagePanel() {
this(null);
}
/**
* @param data
* This parameterized constructor is used to
* add the vector of BufferedImages to JList
*/
public JImagePanel(Vector<BufferedImage> data) {
setLayout(new BorderLayout());
if(data != null) {
listData = data;
}
imageList = new JList();
imageList.setFixedCellHeight(160);
imageList.setFixedCellWidth(160);
if(!listData.isEmpty()) {
imageList.setListData(listData);
}
imageList.setCellRenderer(new JCellRenderer());
imageList.setAutoscrolls(true);
imageList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
imageList.setVisibleRowCount(-1);
scroll_pane = new JScrollPane(imageList);
this.add(scroll_pane, BorderLayout.CENTER);
this.setBorder(new LineBorder(Color.BLUE));
}
/**
* @param selection
* The parameter selection is used to set multi selection on or off
* When true, it allows to select multiple images in JList using
* Ctrl key.
*/
public void setMultiSelection(boolean selection) {
if(selection) {
imageList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
} else {
imageList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
}
public int[] getSelectedIndices() {
return imageList.getSelectedIndices();
}
public int getImageSize() {
return imageList.getModel().getSize();
}
/**
* @param image
* This is used to add BufferedImage to JList
* User can add new images to JList at runtime.
*/
public void addImageToList(BufferedImage image) {
listData.add(image);
imageList.setListData(listData);
}
/**
* The method is used to clear the old list data.
* It sets the list data to empty list.
*/
public void clear() {
listData.clear();
imageList.setListData(listData);
}
/**
* @param data
* This method is used to set list data to JList
* The data should be Vector of BufferedImage
*/
public void setListData(Vector<BufferedImage> data) {
if(data == null || data.isEmpty()) {
System.err.println("Empty Data");
return;
}
listData = data;
imageList.setListData(listData);
}
}
对于此Panel
ListCellRenderer
组件,需要在下面给出。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javax.swing;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.border.EmptyBorder;
/**
*
* @author NiRRaNjAN RauT
*/
/**
* JCellRenderer is extended to JLabel
* It is used to display the image on JList by implementing ListCellRenderer
*/
public class JCellRenderer extends JLabel implements ListCellRenderer {
/**
* The width and height is used to set size of a JLabel.
* It is default set to 200 * 200
*/
private static final int width = 140, height = 140;
/**
* default constructor used to set size of JLabel and set Border to JLabel.
*/
public JCellRenderer() {
setSize(width, height);
setOpaque(true);
setBorder(new EmptyBorder(10, 10, 10, 10));
}
/**
* @return Component
* it returns JLabel for a cell.
* The image from JList is set as icon on JLabel so that it displays the image as a JList item.
*/
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
try {
if(value instanceof BufferedImage) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage((BufferedImage) value, 0, 0, width, height, null);
g.dispose();
setIcon(new ImageIcon(image));
}
if(isSelected) {
setBackground(Color.DARK_GRAY);
} else {
setBackground(Color.LIGHT_GRAY);
}
} catch(Exception ex) {
System.err.println("" + ex.getMessage());
}
return this;
}
}
并且这是如何使用它。
JFrame frame = new JFrame("Image Panel Demo");
frame.setResizable(true);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JImagePanel panel = new JImagePanel();
panel.setMultiSelection(true);
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
try {
File files[] = new File("/SOFTWARES/NETBEANS_PROJECTS/BE/S_CBIR/dataset").listFiles();
for(File file : files) {
BufferedImage image = ImageIO.read(file);
panel.addImageToList(image);
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
希望它能提供帮助。