【问题标题】:How to use JFileChooser to display image in a JPanel如何使用 JFileChooser 在 JPanel 中显示图像
【发布时间】:2013-06-17 15:12:46
【问题描述】:

我正在尝试使用一个菜单栏,使用户能够选择一个文件并将其显示在 JPanel 中,并且图像应该完全适合 JPanel。但是 JFileChooser 在从对话框中成功选择文件后不显示任何内容。我尝试参考许多链接:How to add an image to a JPanel?Browse for image file and display it using Java Swing 但没有任何结果。请帮忙。以下是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;

class Main {
private JFrame j;
private JMenu jmenu;
private JMenuBar jbar;
private JMenuItem jmi, jexit;
private JPanel jpanel, jpanelbar;
private JButton jpre, jnext;
JLabel image;
ImageIcon ic;
Image img;

Main() {
    j = new JFrame("Image Viewer");
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // j.setExtendedState(Frame.MAXIMIZED_BOTH);
    // j.setLocationRelativeTo(null);
    j.setLocationByPlatform(true);
    j.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    jpanel = new JPanel();
    c.anchor = GridBagConstraints.PAGE_START;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = c.gridy = 0;
    c.gridwidth = 2;
    // c.weightx=0.1;
    c.weighty = 0.1;
    c.ipady = 600;
    c.insets = new Insets(5, 5, 10, 5);
    // jpanel.setBackground(Color.BLACK);
    j.add(jpanel, c);

    jpanelbar = new JPanel();
    jpanelbar.setBackground(Color.red);
    c.weightx = 0.1;
    c.gridx = 0;
    c.gridy = 1;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(5, 5, 5, 5);
    c.ipady = 150;
    j.add(jpanelbar, c);

    jpanelbar.setLayout(new GridBagLayout());
    GridBagConstraints x = new GridBagConstraints();
    jpre = new JButton("Previous");
    x.gridx = 0;
    x.gridy = 0;
    x.gridwidth = 1;
    x.weightx = 0.1;
    // x.insets=new Insets(5,5,5,5);
    // x.fill=GridBagConstraints.NONE;
    jpanelbar.add(jpre, x);

    jnext = new JButton("Next");
    x.gridx = 1;
    jpanelbar.add(jnext, x);

    // Creating Menu
    jbar = new JMenuBar();
    jmenu = new JMenu("File");
    jmi = new JMenuItem("Open");
    jmi.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser fc = new JFileChooser();
            int result = fc.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                String sname = file.getName();
                image = new JLabel("", new ImageIcon(sname), JLabel.CENTER);
                jpanel.add(image, BorderLayout.CENTER);
            }
        }
    });
    jexit = new JMenuItem("Exit");
    jexit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            System.exit(0);
        }
    });
    jmenu.add(jmi);
    jmenu.add(jexit);
    jbar.add(jmenu);
    j.setJMenuBar(jbar);

    j.setSize(800, 600);
    j.setResizable(false);
    j.setVisible(true);
}

public static void main(String s[]) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new Main();
        }
    });
}
}

更新后的代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;

class Main {
private JFrame j;
private JMenu jmenu;
private JMenuBar jbar;
private JMenuItem jmi, jexit;
private JPanel jpanel, jpanelbar;
private JButton jpre, jnext;
JLabel image;
ImageIcon ic;
Image img;

Main() {
    j = new JFrame("Image Viewer");
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // j.setExtendedState(Frame.MAXIMIZED_BOTH);
    // j.setLocationRelativeTo(null);
    j.setLocationByPlatform(true);
    j.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    jpanel = new JPanel();
    c.anchor = GridBagConstraints.PAGE_START;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = c.gridy = 0;
    c.gridwidth = 2;
    // c.weightx=0.1;
    c.weighty = 0.1;
    c.ipady = 600;
    c.insets = new Insets(5, 5, 10, 5);
    // jpanel.setBackground(Color.BLACK);
    j.add(jpanel, c);

    jpanelbar = new JPanel();
    jpanelbar.setBackground(Color.red);
    c.weightx = 0.1;
    c.gridx = 0;
    c.gridy = 1;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(5, 5, 5, 5);
    c.ipady = 150;
    j.add(jpanelbar, c);

    jpanelbar.setLayout(new GridBagLayout());
    GridBagConstraints x = new GridBagConstraints();
    jpre = new JButton("Previous");
    x.gridx = 0;
    x.gridy = 0;
    x.gridwidth = 1;
    x.weightx = 0.1;
    // x.insets=new Insets(5,5,5,5);
    // x.fill=GridBagConstraints.NONE;
    jpanelbar.add(jpre, x);

    jnext = new JButton("Next");
    x.gridx = 1;
    jpanelbar.add(jnext, x);

    // Creating Menu
    jbar = new JMenuBar();
    jmenu = new JMenu("File");
    jmi = new JMenuItem("Open");
    jmi.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser fc = new JFileChooser();
            int result = fc.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                String sname = file.getName();
                image = new JLabel("", new ImageIcon(sname), JLabel.CENTER);
                jpanel.add(image, BorderLayout.CENTER);
                jpanel.revalidate();
                jpanel.repaint();
            }
        }
    });
    jexit = new JMenuItem("Exit");
    jexit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            System.exit(0);
        }
    });
    jmenu.add(jmi);
    jmenu.add(jexit);
    jbar.add(jmenu);
    j.setJMenuBar(jbar);

    j.setSize(800, 600);
    j.setResizable(false);
    j.setVisible(true);
}

public static void main(String s[]) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new Main();
        }
    });
}
}

【问题讨论】:

    标签: java image swing jpanel jfilechooser


    【解决方案1】:

    你需要调用

    jpanel.revalidate();
    jpanel.repaint();
    

    在将JLabel image 添加到jpanel 之后,为什么不简单地添加JLabel 在启动时使用setIcon 来设置Image

    【讨论】:

    • 添加这些行后仍然没有显示。而且我没有得到你指定的第二种方法。
    • repaint 自 JDK 1.0 以来就存在。你能发布你更新的代码吗?
    • @crazy4 随着revalidate 调整面板jpanel 的大小,您的按钮正在消失。如果您致电j.pack(),您会看到首选尺寸。问题在于较大的 ipady 设置 - 重新验证按钮面板后被推到了 JFrame 的范围之外。
    【解决方案2】:

    file 对象上使用getAbsolutePath() 而不是getName(),并在渲染图像后调用repaint()revalidate()

    以下代码应该可以解决您的问题:

    jmi.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser fc = new JFileChooser();
            int result = fc.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                String sname = file.getAbsolutePath(); //THIS WAS THE PROBLEM
                image = new JLabel("", new ImageIcon(sname), JLabel.CENTER);
                jpanel.add(image, BorderLayout.CENTER);
                jpanel.revalidate(); //ADD THIS AS WELL
                jpanel.repaint();  //ADD THIS AS WELL
            }
        }
    });
    

    【讨论】:

    • 嘿!谢谢。这样就成功了。但是你能解释一下你到底做了什么吗?请。
    • 您使用的是file.getName(),这将使渲染过程在当前目录中查找图像。我将其更改为file.getAbsolutePath()。除了@Reimeus 提到的,您应该调用revalidate() 和/或repaint() 以使UI 更改生效。另外,如果它解决了您的问题,请将其标记为答案。
    • 现在,在 repaint() 和 revalidate() 之后,下面板和按钮消失了。为什么?我想显示图像而不会丢失它们。
    • 如果图像的大小大于框架的大小,就会发生这种情况。使用较小的图像进行测试。要了解如何调整图像大小以使其适合 JLabel,请参阅此link
    • 我尝试过使用较小的图像,即使图像分辨率为 110x25 像素。但是下面的按钮没有出现在那里。
    【解决方案3】:

    我让你的代码半工作。还有很多很多的问题需要解决。

    • 我在您的 jpanel 中添加了 BorderLayout。

    • 我将图像的初始化从您打开的菜单动作侦听器中移出,就像 Reimeus 告诉您的那样。

    • 我使用 ImageIO 读取图像。

    你最终会需要这个答案。 Resize a picture to fit a JLabel

    这是我的代码版本。愿上帝怜悯你的灵魂。

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Image;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    class Main {
        private JFrame      j;
        private JMenu       jmenu;
        private JMenuBar    jbar;
        private JMenuItem   jmi, jexit;
        private JPanel      jpanel, jpanelbar;
        private JButton     jpre, jnext;
        JLabel              image;
        ImageIcon           ic;
        Image               img;
    
        Main() {
            j = new JFrame("Image Viewer");
            j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // j.setExtendedState(Frame.MAXIMIZED_BOTH);
            // j.setLocationRelativeTo(null);
            j.setLocationByPlatform(true);
            j.setLayout(new GridBagLayout());
    
            GridBagConstraints c = new GridBagConstraints();
            jpanel = new JPanel();
            jpanel.setLayout(new BorderLayout());
            image = new JLabel(" ");
            jpanel.add(image, BorderLayout.CENTER);
    
            c.anchor = GridBagConstraints.PAGE_START;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = c.gridy = 0;
            c.gridwidth = 2;
            // c.weightx=0.1;
            c.weighty = 0.1;
            c.ipady = 600;
            c.insets = new Insets(5, 5, 10, 5);
            // jpanel.setBackground(Color.BLACK);
            j.add(jpanel, c);
    
            jpanelbar = new JPanel();
            jpanelbar.setLayout(new GridBagLayout());
            jpanelbar.setBackground(Color.red);
    
            GridBagConstraints x = new GridBagConstraints();
            jpre = new JButton("Previous");
            x.gridx = 0;
            x.gridy = 0;
            x.gridwidth = 1;
            x.weightx = 0.1;
            // x.insets=new Insets(5,5,5,5);
            // x.fill=GridBagConstraints.NONE;
            jpanelbar.add(jpre, x);
    
            jnext = new JButton("Next");
            x.gridx = 1;
            jpanelbar.add(jnext, x);
    
            c.weightx = 0.1;
            c.gridx = 0;
            c.gridy = 1;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.insets = new Insets(5, 5, 5, 5);
            c.ipady = 150;
            j.add(jpanelbar, c);
    
            // Creating Menu
            jbar = new JMenuBar();
            jmenu = new JMenu("File");
            jmi = new JMenuItem("Open");
            jmi.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    JFileChooser fc = new JFileChooser();
                    int result = fc.showOpenDialog(null);
                    if (result == JFileChooser.APPROVE_OPTION) {
                        File file = fc.getSelectedFile();
                        try {
                            image.setIcon(new ImageIcon(ImageIO.read(file)));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            jexit = new JMenuItem("Exit");
            jexit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    System.exit(0);
                }
            });
            jmenu.add(jmi);
            jmenu.add(jexit);
            jbar.add(jmenu);
            j.setJMenuBar(jbar);
    
    //      j.setSize(800, 600);
            j.pack();
            j.setResizable(true);
            j.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new Main();
                }
            });
        }
    }
    

    【讨论】:

    • 此代码生成“不支持的图像类型”错误。有什么解决办法吗?
    猜你喜欢
    • 2016-10-09
    • 2011-10-07
    • 1970-01-01
    • 2016-07-23
    • 2011-05-05
    • 2013-02-25
    • 1970-01-01
    • 2017-08-02
    • 2017-01-10
    相关资源
    最近更新 更多