刚开始使用 Java 并且是初学者。我尝试创建一个照片查看器,它可以在目录中搜索图像并打开图像,但我的程序不会显示图像。
当我运行程序时,它会打开并显示一个菜单栏,我用它来搜索我的目录,但即使我选择了一个图像,它也不会显示。TIA。
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 ICS
{
private JPanel gui;
private JFileChooser fileChooser;
FilenameFilter fileNameFilter;
private JMenuBar menuBar;
DefaultListModel model;
public ICS() {
gui = new JPanel(new GridLayout());
final JLabel imageView = new JLabel();
gui.add(imageView);
fileChooser = new JFileChooser();
String[] imageTypes = ImageIO.getReaderFileSuffixes();
menuBar = new JMenuBar();
JMenu menu = new JMenu("GET PHOTO HERE");
menuBar.add(menu);
JMenuItem browse = new JMenuItem("browse");
menu.add(browse);
browse.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
int result = fileChooser.showOpenDialog(gui);
if (result==JFileChooser.APPROVE_OPTION) {
File eg = fileChooser.getSelectedFile();
}
}
});
}
public void loadImages(File directory) throws IOException {
File[] imageFiles = directory.listFiles(fileNameFilter);
BufferedImage[] images = new BufferedImage[imageFiles.length];
}
public Container getGui() {
return gui;
}
public JMenuBar getMenuBar() {
return menuBar;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
ICS imageList = new ICS();
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);
}
});
}
}