简体   繁体   English

图像不会显示在JPanel上

[英]Image won't display on a JPanel

Just started out using Java and am a beginner. 刚开始使用Java并且是初学者。 I tried to create a photo viewer which can search a directory for an image and open the image but my program won't display the image. 我尝试创建一个照片查看器,可以在目录中搜索图像并打开图像,但我的程序不会显示图像。

When i run the program, it opens up and shows a menubar which i use to search my directories, but even if i select an image it won't display. 当我运行该程序时,它打开并显示一个菜单栏,我用它来搜索我的目录,但即使我选择了一个图像,它也不会显示。 TIA. 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);
            }
        });
}

} }

You're not doing anything with your selected file. 您没有对所选文件执行任何操作。 Given that you have an empty JLabel in your JPanel , you could simply set the Icon for that component: 假设您的JPanel有一个空的JLabel ,您可以简单地为该组件设置Icon

imageView.setIcon(new ImageIcon(eg.getPath()));

It's not doing anything with the image because you've never told it to. 它没有对图像做任何事情,因为你从未告诉它。 In your action listener, you've created a file chooser and gotten the selected file, but you never do anything with it. 在您的动作侦听器中,您已经创建了一个文件选择器并获得了所选文件,但您从未对其执行任何操作。 You just define it as a local variable inside the action listener, which is then immediately destroyed when your listener exits. 您只需将其定义为动作侦听器中的局部变量,然后在侦听器退出时立即将其销毁。

What you should be doing is, inside of your action listener, make a function call that actually displays your image after you retrieve the file the user selected. 您应该做的是,在您的动作侦听器内部,在您检索用户选择的文件后,进行实际显示图像的函数调用。

Also, ICS is a terrible name for a class. 此外,ICS是一个可怕的名字。 You should be descriptive of your class names for your own reference and sanity when your program gets larger and you're trying to remember what everything did. 当你的程序变大并且你想要记住所做的一切时,你应该描述你的类名,以供你自己参考和理智。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM