import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
public class ElectronicAlbum extends JFrame {
private JLabel photoLabel;
private ImageIcon[] photos = new ImageIcon[0]; // 初始化为空数组
private int currentIndex = 0;
public ElectronicAlbum() {
setSize(800, 600);
setTitle("电子相册");
JMenuBar menuBar = new JMenuBar();
getContentPane().add(menuBar, BorderLayout.NORTH);
JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 3));
scrollPane.setViewportView(panel);
JMenuItem openItem = new JMenuItem("打开照片");
JMenuItem prevItem = new JMenuItem("上一页");
JMenuItem nextItem = new JMenuItem("下一页");
JMenuItem exitItem = new JMenuItem("退出");
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser(".");
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
if (file.exists()) { // 检查文件是否存在
ImageIcon newPhoto = new ImageIcon(file.getPath());
ImageIcon[] newPhotos = new ImageIcon[photos.length + 1];
System.arraycopy(photos, 0, newPhotos, 0, photos.length);
newPhotos[photos.length] = newPhoto;
photos = newPhotos;
currentIndex = photos.length - 1; // 更新当前图片索引
updatePhoto(); // 更新界面显示
pack(); // 重新布局窗口大小
} else {
JOptionPane.showMessageDialog(null, "文件不存在!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
});
prevItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentIndex > 0) {
currentIndex--;
updatePhoto();
}
}
});
nextItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentIndex < photos.length - 1) {
currentIndex++;
updatePhoto();
}
}
});
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menuBar.add(openItem);
menuBar.add(prevItem);
menuBar.add(nextItem);
menuBar.add(exitItem);
photoLabel = new JLabel();
getContentPane().add(photoLabel, BorderLayout.SOUTH);
updatePhoto();
setVisible(true);
setLocationRelativeTo(null);
}
private void updatePhoto() {
if (photos != null && currentIndex >= 0 && currentIndex < photos.length) {
photoLabel.setIcon(photos[currentIndex]);
}
}
public static void main(String[] args) {
new ElectronicAlbum();
}
}
出现了该报错
可以这样解决:
1. 在用户选择文件后,加一个文件存在判断,如果文件不存在,弹出错误提示,不继续处理:
java
if (file.exists()) {
// 文件存在,继续处理
} else {
JOptionPane.showMessageDialog(null, "文件不存在!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
2. 也可以捕获FileNotFoundException异常,并提示用户文件不存在:
java
try {
ImageIcon newPhoto = new ImageIcon(file.getPath());
// 继续处理
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, "文件不存在!", "错误", JOptionPane.ERROR_MESSAGE);
}
3. 另外,在更新图片索引currentIndex时,也要判断索引是否越界,如果越界就不更新索引:
java
if (currentIndex < photos.length - 1) {
currentIndex++;
} else {
JOptionPane.showMessageDialog(null, "已经是最后一张图片!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
以上几点修改后,这个电子相册程序就可以完美运行了。