我有一个应用程序来显示用户数据的图像。每个用户拥有超过 1 个图像数据。当我在 JTable 中选择用户时,我的图像数据会显示在 JPanel 中。该图片实际上还带有背景图片的JPanel。
我的问题是,内存消耗总是增加,而不是减少。有时应用程序会挂起。如何将图片(JPanel)注册到垃圾收集。如果可能的话,我什么时候必须注册它们?我是 Java 性能问题的新手。
这是我的代码:
public void getStreamData(final PanelEntry view, final String data) {
String files = null;
String path = null;
if(isImages()) {
path = "data/"+data+"/images";
} else {
path = "data/"+data+"/videos/thumbs";
}
File folder = new File(path);
// Always remove previous image label when new data selected
view.getPanelStream().removeAll();
if (!folder.exists()) {
JLabel label = new JLabel("No Stream Data");
label.setForeground(Color.red);
label.setVisible(true);
// Adding to panelGallery
view.getPanelStream().add(label);
view.getPanelStream().revalidate();
view.getPanelStream().repaint();
} else {
File [] listOfFiles = folder.listFiles();
int maxFiles = listOfFiles.length;
int maxView = 15;
// Loop for get image from file
for (int i = listOfFiles.length; i > 0 ; i--) {
if(listOfFiles[i].isFile()) {
files = listOfFiles[i].getName();
final String videoFiles = files;
if(files.endsWith(".jpg") || files.endsWith(".JPG") ||
files.endsWith(".jpeg") || files.endsWith(".JPEG") ||
files.endsWith(".png") || files.endsWith(".PNG")) {
final String newPath = path+"/"+files;
try {
File showFile = new File(newPath);
ImageIcon imgSource = new ImageIcon(newPath);
JPanel labelGallery = new BackgroundImageRounded(showFile);
labelGallery.setLayout(null);
labelGallery.setPreferredSize(new Dimension(160, 120));
labelGallery.setVisible(true);
JLabel labelName = new JLabel(files);
labelName.setSize(150,15);
labelName.setLocation(8, 8);
labelName.setVisible(true);
labelName.setForeground(Color.ORANGE);
labelGallery.add(labelName);
String videoPath = "data/"+data+"/videos/";
String video = videoFiles.replace(".jpg", ".wmv");
String videoFile = video.replace("thumb_", "video_");
final String videoPlayer = videoPath+videoFile;
if (isImages()) {
labelGallery.setToolTipText("View Image");
} else {
labelGallery.setToolTipText("Play Video");
JLabel iconPlayer = new JLabel();
iconPlayer.setIcon(new ImageIcon(getClass().getResource("/com/ikbiz/gastroscope/resources/player.png")));
iconPlayer.setSize(61,42);
iconPlayer.setVisible(true);
iconPlayer.setLocation(50, 35);
labelGallery.add(iconPlayer);
}
labelGallery.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(isImages()) {
ImageViewer viewer = new ImageViewer(newPath);
viewer.setVisible(true);
} else {
VideoViewer videoViewer = new VideoViewer();
videoViewer.setViewer(videoPlayer);
videoViewer.setLocationRelativeTo(null);
videoViewer.pack();
videoViewer.setVisible(true);
}
}
});
// Adding to panelGallery
view.getPanelStream().add(labelGallery);
view.getPanelStream().revalidate();
view.getPanelStream().repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
请帮忙,
谢谢你。