我有一个JPanel
我想在该面板中添加多个图像并提供一个垂直滚动来显示图像。图像可能具有不同的大小。
我怎样才能做到这一点?
我有一个JPanel
我想在该面板中添加多个图像并提供一个垂直滚动来显示图像。图像可能具有不同的大小。
我怎样才能做到这一点?
对于大小不等的图像,在 a 中使用文本和/或“小图标”渲染器JList
,并将其放在PAGE_START
a 中BorderLayout
。JLabel
给它添加一个监听器,然后在 a中的 aJScrollPane
中显示选中的图像CENTER
。像这样的东西:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.imageio.ImageIO;
public class ImageList {
private JPanel gui;
private JFileChooser fileChooser;
FilenameFilter fileNameFilter;
private JMenuBar menuBar;
DefaultListModel model;
ImageList() {
gui = new JPanel(new GridLayout());
JPanel imageViewContainer = new JPanel(new GridBagLayout());
final JLabel imageView = new JLabel();
imageViewContainer.add(imageView);
model = new DefaultListModel();
final JList imageList = new JList(model);
imageList.setCellRenderer(new IconCellRenderer());
ListSelectionListener listener = new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent lse) {
Object o = imageList.getSelectedValue();
if (o instanceof BufferedImage) {
imageView.setIcon(new ImageIcon((BufferedImage)o));
}
}
};
imageList.addListSelectionListener(listener);
fileChooser = new JFileChooser();
String[] imageTypes = ImageIO.getReaderFileSuffixes();
FileNameExtensionFilter fnf = new FileNameExtensionFilter("Images", imageTypes);
fileChooser.setFileFilter(fnf);
File userHome = new File(System.getProperty("user.home"));
fileChooser.setSelectedFile(userHome);
fileNameFilter = new FilenameFilter() {
@Override
public boolean accept(File file, String name) {
return true;
}
};
menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem browse = new JMenuItem("Browse");
menu.add(browse);
browse.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
int result = fileChooser.showOpenDialog(gui);
if (result==JFileChooser.APPROVE_OPTION) {
File eg = fileChooser.getSelectedFile();
// this will be an image, we want the parent directory
File dir = eg.getParentFile();
try {
loadImages(dir);
} catch(Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(
gui,
e,
"Load failure!",
JOptionPane.ERROR_MESSAGE);
}
}
}
});
gui.add(new JSplitPane(
JSplitPane.HORIZONTAL_SPLIT,
new JScrollPane(
imageList,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),
new JScrollPane(imageViewContainer)));
}
public void loadImages(File directory) throws IOException {
File[] imageFiles = directory.listFiles(fileNameFilter);
BufferedImage[] images = new BufferedImage[imageFiles.length];
model.removeAllElements();
for (int ii=0; ii<images.length; ii++) {
model.addElement(ImageIO.read(imageFiles[ii]));
}
}
public Container getGui() {
return gui;
}
public JMenuBar getMenuBar() {
return menuBar;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
ImageList imageList = new ImageList();
JFrame f = new JFrame("Image Browser");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.add(imageList.getGui());
f.setJMenuBar(imageList.getMenuBar());
f.setLocationByPlatform(true);
f.pack();
f.setSize(800,600);
f.setVisible(true);
}
});
}
}
class IconCellRenderer extends DefaultListCellRenderer {
private static final long serialVersionUID = 1L;
private int size;
private BufferedImage icon;
IconCellRenderer() {
this(100);
}
IconCellRenderer(int size) {
this.size = size;
icon = new BufferedImage(size,size,BufferedImage.TYPE_INT_ARGB);
}
@Override
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (c instanceof JLabel && value instanceof BufferedImage) {
JLabel l = (JLabel)c;
l.setText("");
BufferedImage i = (BufferedImage)value;
l.setIcon(new ImageIcon(icon));
Graphics2D g = icon.createGraphics();
g.setColor(new Color(0,0,0,0));
g.clearRect(0, 0, size, size);
g.drawImage(i,0,0,size,size,this);
g.dispose();
}
return c;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(size, size);
}
}
如果图像大小相同,请参阅此答案。(在规范澄清之前,这是答案的顶部。我留下了它,因为它是一个非常可爱的屏幕截图。)
结合评论对这个问题做出有用的回答:
添加多个组件,如JLabel
s inJPanel
并将图像设置为它们的图标,将整个面板添加到JScrollPane
. 组件的层次结构如下:
JFrame
/JWindow
->JScrollPane
->JPanel
->JLabel(s)
其中->代表'包含'。