在运行时在标签中显示图像

时间:2013-06-07 16:00:50

标签: java image swing jlabel jfilechooser

我想要的是在用户从文件选择器中选择它后在标签中显示图像,我遵循了很多教程,其中一个是https://netbeans.org/kb/docs/java/gui-image-display.html当我对图像名称进行硬编码时它的工作原理,但我真正需要的是显示用户从文件选择器中选择的内容。我试过这个     int retV;         文件file = null;

    if (fc== null)
        fc=new JFileChooser();

    fc.addChoosableFileFilter(new ImageFilter());
    fc.setAcceptAllFileFilterUsed(false);

    retV =fc.showOpenDialog(GUI.this);
    if (retV == JFileChooser.APPROVE_OPTION)
         file = fc.getSelectedFile();
    infoText.append("-The image : "+file.getAbsolutePath()+"\nwill be conveted to PNG format becuase it's a lossless\n compression.\n");

    ImageIcon icon= new ImageIcon(file.getAbsolutePath());
    img1=new JLabel(icon);

但它不起作用。

非常感谢任何帮助

3 个答案:

答案 0 :(得分:2)

另一个选项是将选定的File转换为URL

selectedFile.toURI().toURL()

SSCCE

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class TrivialImageViewer implements Runnable {

    private static final Font DEMO_FONT = new Font("Arial", Font.BOLD, 20);

    private final JFrame frame;
    private final JFileChooser fileChooser;
    private final JLabel iconLabel;

    private final class SelectImageAction extends AbstractAction {
        private static final long serialVersionUID = 1L;

        private SelectImageAction(final String name) {
            super(name);
        }

        @Override
        public void actionPerformed(final ActionEvent e) {

            final int option = fileChooser.showOpenDialog(frame);
            if (option != JFileChooser.APPROVE_OPTION) {
                return;
            }

            final File selectedFile = fileChooser.getSelectedFile();

            URL url;
            try {
                url = selectedFile.toURI().toURL();
            } catch (final MalformedURLException e1) {
                throw new RuntimeException(e1);
            }

            final ImageIcon icon= new ImageIcon(url);
            iconLabel.setText("");
            iconLabel.setIcon(icon);
        }
    }

    public TrivialImageViewer() {
        frame = new JFrame();
        frame.setTitle("Trivial Swing ImageViewer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        iconLabel = new JLabel("Please select an image.");
        iconLabel.setFont(DEMO_FONT);
        iconLabel.setHorizontalAlignment(SwingConstants.CENTER);

        fileChooser = new JFileChooser();

        frame.add(iconLabel, BorderLayout.CENTER);
        frame.add(new JButton(new SelectImageAction("Select Image...")), BorderLayout.SOUTH);
    }

    @Override
    public void run() {
        frame.setBounds(16, 16, 640, 480);
        frame.setVisible(true);
    }

    public static void main(final String[] args) throws Exception {
                    // Use EventQueue.invokeLater in serious apps...
        final TrivialImageViewer application = new TrivialImageViewer();
        application.run();
    }
}

如果你在教室外使用它,不要忘记实施适当的资源管理,并且:玩得开心!

答案 1 :(得分:1)

根据您提供的信息......目前尚不清楚故障是什么......但是为了避免我们通过路径......和绝对路径获得的那些问题......请改用以下技术。

您可以通过任何方式获取File对象。因此,使用该对象读取文件并创建BufferedImage,它是Image的子类,并将其传递给构造函数。使用对象时不要忘记检查null .....

改为使用:

  File file=fc.getSelectedFile();
  BufferedImage bImage= ImageIO.read(file);
  ImageIcon icon= new ImageIcon(img);

这是解决方案之一.... 我希望这有帮助......

答案 2 :(得分:1)

试试这个:

ImageIcon ii=new ImageIcon("filename");
JLabel label=new JLabel();
label.setIcon(ii);
相关问题