如何从文件夹中的图像将图标设置为 JLabel?

新手上路,请多包涵

每当从 JComboBox 中选择一个项目时,我都试图从图像文件夹中为 JLabel 设置一个图标。 JComboBox 中项目的名称和文件夹中图像的名称相同。因此,无论何时从 JComboBox 中选择一个项目,都应将具有相同名称的相应图像设置为 JLabel 的图标。我正在尝试做这样的事情。

 private void cmb_movieselectPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt){
        updateLabel(cmb_moviename.getSelectedItem().toString());
}

protected void updateLabel(String name) {
        ImageIcon icon = createImageIcon("C:\\Users\\xerof_000\\Pictures\\tmspictures\\" + name + ".jpg");
        if(icon != null){
            Image img = icon.getImage();
            Image newimg = img.getScaledInstance(lbl_pic.getWidth(), lbl_pic.getHeight(),  java.awt.Image.SCALE_SMOOTH);
            icon = new ImageIcon(newimg);
            lbl_pic.setIcon(icon);
            lbl_pic.setText(null);
        }
        else{
            lbl_pic.setText("Image not found");
            lbl_pic.setIcon(null);
        }
    }

protected static ImageIcon createImageIcon(String path) {
        URL imgURL;
        imgURL = NowShowing.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            return null;
        }
    }

我认为问题出在“C:\Users\xerof_000\Pictures\tmspictures\”中,我尝试使用“C:/Users/xerof_000/Pictures/tmspictures/”,但即使那样也不起作用。无论我做什么,它只会在 JLabel 上显示“找不到图像”。

原文由 Raed Shahid 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 368
2 个回答

这是我的目录结构:

                                 packageexample
                                      |
                   -------------------------------------------
                   |                  |                      |
                build(folder)     src(folder)           manifest.txt
                   |                  |
             swing(package)       ComboExample.java
                   |
            imagetest(subpackage)
                   |
     ComboExample.class + related .class files

这是 ComboExample.java 文件的内容:

 package swing.imagetest;

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;

public class ComboExample {

    private String[] data = new String[]{
                                            "geek0",
                                            "geek1",
                                            "geek2",
                                            "geek3",
                                            "geek4"
                                        };
    private String MESSAGE = "No Image to display yet...";
    private JLabel imageLabel;
    private JComboBox cBox;
    private ActionListener comboActions =
                            new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            JComboBox combo = (JComboBox) ae.getSource();
            ImageIcon image = new ImageIcon(
                        getClass().getResource(
                            "/" + combo.getSelectedItem() + ".gif"));
            if (image != null) {
                imageLabel.setIcon(image);
                imageLabel.setText("");
            } else {
                imageLabel.setText(MESSAGE);
            }
        }
    };

    private void displayGUI() {
        JFrame frame = new JFrame("Combo Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        imageLabel = new JLabel(MESSAGE, JLabel.CENTER);
        cBox = new JComboBox(data);
        cBox.addActionListener(comboActions);

        contentPane.add(imageLabel);
        contentPane.add(cBox);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ComboExample().displayGUI();
            }
        });
    }
}

现在编译:

为了编译我这样做了:

 Gagandeep Bali@LAPTOP ~/c/Mine/JAVA/J2SE/src/packageexample
$ javac -d build src/*.java

清单文件的内容:

在此处输入图像描述

JAR 文件创建:

 Gagandeep Bali@LAPTOP ~/c/Mine/JAVA/J2SE/src/packageexample
$ cd build

Gagandeep Bali@LAPTOP ~/c/Mine/JAVA/J2SE/src/packageexample/build
$ jar -cfm imagecombo.jar ../manifest.txt *

现在将这个 JAR File 带到任何有这些图像的位置(极客0.gif ,极客1.gif ,极客2.gif ,极客3.gif极客4.gif), 并运行 JAR File ,然后查看结果:-)

原文由 nIcE cOw 发布,翻译遵循 CC BY-SA 3.0 许可协议

正如 如何使用图标 中所讨论的, getResource() 方法期望在程序的 JAR 文件中找到图像。您需要将图像移动到您的项目中。 IconDemo 是一个完整的例子。

原文由 trashgod 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题