可以缩成所有的图像使用较少的内存。例如此代码将所有图像缩小到200x200。这样你就可以放入100MB的1000张图片。
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.File;
public class Scroll extends JPanel {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
JPanel panel = new Scroll();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for(int i = 0; i < 10; i++) {
JPanel buttonPanel = new JPanel();
JRadioButton b1 = new JRadioButton("button 1");
JRadioButton b2 = new JRadioButton("button 2");
JRadioButton b3 = new JRadioButton("button 3");
ButtonGroup group = new ButtonGroup();
group.add(b1);
group.add(b2);
group.add(b3);
buttonPanel.add(b1);
buttonPanel.add(b2);
buttonPanel.add(b3);
BufferedImage buffer = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffer.createGraphics();
BufferedImage image = ImageIO.read(new File("image.jpg"));
g.scale(buffer.getWidth()*1.0/image.getWidth(),
buffer.getHeight()*1.0/image.getHeight());
g.drawImage(image, 0, 0, null);
g.dispose();
JLabel imageLabel = new JLabel(new ImageIcon(buffer));
JSplitPane splitPane = new JSplitPane();
splitPane.add(imageLabel, JSplitPane.LEFT);
splitPane.add(buttonPanel, JSplitPane.RIGHT);
panel.add(splitPane);
}
JScrollPane spane = new JScrollPane(panel);
frame.add(spane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,600);
frame.setVisible(true);
}
}
如果要动态地加载图像,因为它们变得可见,你必须使用空JPanels为每个图像,而不是ImageIcons,然后你会覆盖JPanels的paint方法。只有JPanel可见时才会调用paint方法。所以最简单的解决方案是始终在绘制方法中从磁盘加载图像,然后将其绘制到屏幕上。
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.File;
public class Scroll extends JPanel {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
JPanel panel = new Scroll();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for(int i = 0; i < 10; i++) {
JPanel buttonPanel = new JPanel();
JRadioButton b1 = new JRadioButton("button 1");
JRadioButton b2 = new JRadioButton("button 2");
JRadioButton b3 = new JRadioButton("button 3");
ButtonGroup group = new ButtonGroup();
group.add(b1);
group.add(b2);
group.add(b3);
buttonPanel.add(b1);
buttonPanel.add(b2);
buttonPanel.add(b3);
JPanel imagePanel = new JPanel() {
{
BufferedImage image = ImageIO.read(new File("image.jpg"));
setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
image.flush();
}
@Override
public void paint(Graphics g) {
try {
BufferedImage image = ImageIO.read(new File("image.jpg"));
g.drawImage(image, 0, 0, null);
image.flush();
} catch(Exception e) {
}
}
};
JSplitPane splitPane = new JSplitPane();
splitPane.add(imagePanel, JSplitPane.LEFT);
splitPane.add(buttonPanel, JSplitPane.RIGHT);
panel.add(splitPane);
}
JScrollPane spane = new JScrollPane(panel);
frame.add(spane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,600);
frame.setVisible(true);
}
}
*“大jpgs,每个大约7-8MB”*取决于压缩,这可能导致大量的最终像素大小。图像是什么样的WxH? –
@AndrewThompson,约2600 * 3900,相反。 –