【问题标题】:Displaying an image in a label at runtime在运行时在标签中显示图像
【发布时间】:2013-06-04 00:17:14
【问题描述】:

我想要的是在用户从文件选择器中选择图像后在标签中显示图像,我遵循了很多教程,其中之一是 https://netbeans.org/kb/docs/java/gui-image-display.html 它在我硬编码图像名称时起作用,但是我真正需要的是显示用户从文件选择器中选择的内容。我试过这个 诠释 retV; 文件文件=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);

但它没有用。

非常感谢任何帮助

【问题讨论】:

标签: java image swing jlabel jfilechooser


【解决方案1】:

根据您提供的信息...目前尚不清楚问题出在哪里...但是为了避免我们通过路径...和绝对路径遇到的问题...请改用以下技术。

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

改用:

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

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

【讨论】:

    【解决方案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();
        }
    }
    

    如果您在课堂外使用它,请不要忘记实施适当的资源管理:玩得开心!

    【讨论】:

      【解决方案3】:

      试试这个:

      ImageIcon ii=new ImageIcon("filename");
      JLabel label=new JLabel();
      label.setIcon(ii);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-16
        • 1970-01-01
        • 2012-11-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多