当前位置: 首页 > 知识库问答 >
问题:

如何使用JFileChooser在JPanel中显示图像

曾修真
2023-03-14

我正在尝试使用一个菜单栏,使用户能够选择一个文件,并在JPanel中显示它,图像应该完全适合JPanel。但是JFileChooser在从对话框中成功选择文件时不会显示任何内容。我试着参考了许多链接:如何向JPanel添加图像?浏览图像文件并使用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();
        }
    });
}
}

共有1个答案

归和惬
2023-03-14

我已经把你的代码搞定了。还有很多很多的问题要解决。

>

  • 我在JPanel中添加了一个BorderLayout。

    我把image的初始化移出了你的open menu action listener,就像Reimeus告诉你的那样。

    我用ImageIO读取图像。

    你最终会需要这个答案的。调整图片大小以适应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();
                }
            });
        }
    }
    

  •  类似资料:
    • 因此,我试图在NetBeans IDE中创建swing gui。我对java和GUI的构建是新手,所以这是一个学习曲线。 我创建了一个名为“open”的JMenuItem,并使用JFileChooser打开并显示一个文件。我限制自己使用的文件扩展名是“*.map”。 所以我让我的代码工作了,它在我的桌面上的一个新窗口中打开了选定的文件。但我想知道如何打开JFrame中的文件,而不是一个新窗口。它不

    • 问题内容: 我想创建一个带有缩略图的图像文件视图,所以我将FileView子类化,并在创建方法中进行了一些缩放,以便显示缩略图图像。 但是,总体效果是,该小部件在打开目录并显示缩略图之前需要花费一些时间。在下面的createImageIcon()中,我需要两次调用new ImageIcon()两次,分别使用图像文件路径和下一次调整大小的图像作为构造函数参数。我认为这是使小部件变慢的原因。 有没有更

    • 问题内容: 在JPanel上显示jpg图像(从本地文件夹加载)的最合适的图像类型是什么? 干杯。 问题答案:

    • 问题内容: 如何在中显示文件的默认系统图标?即文件中的图标应与桌面和资源管理器中显示的图标相同? 例如,NetBeans图标的外观将与桌面上的外观不同! 这该怎么做? 问题答案: 我们可以通过调用类中的静态方法来使用该类并获取其对象,然后使用该方法获取一个对象并返回其图标。 和类存在于包中。 类在包中。 注意: 不扩展。因此,您不能在 代表当前帧。假设编写此代码的类是的子类 或者以一种简单的方式

    • 问题内容: 我是的新手。在我的应用程序中,我想在或中显示。 谁能帮我吗? 问题答案:

    • 问题内容: 我正在编写一个需要文件打开对话框的Java程序。文件打开对话框并不难,我希望使用。我的问题是我想要一个双窗格(由2组成)。左侧面板上有一个,右侧面板上有一个文件打开对话框。 当我使用它时,将在所有其他窗口上方打开对话框,这不是我想要的。有什么方法可以使(或可能是另一个文件选择对话框)显示在而不在其上方弹出? 这是我尝试过的代码,目前已非常简化。目前,我只是试图将嵌入。 我也打过电话与和