java - 将图标设置为 JLabel - 不显示图标

标签 java image swing jlabel embedded-resource

我的代码:

public class UserDialog extends JDialog {

    public UserDialog() {
    add(createForm(), BorderLayout.CENTER);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setLocation(400, 100);
    pack();
    setVisible(true);
}

    public JPanel createForm() {
    JPanel panel = new JPanel();
    ImageIcon image = new ImageIcon("Check.png");

    okBtn = new JButton("Ok");
    cancelBtn = new JButton("Cancel");
    tempBtn = new JLabel();
    fNameLbl = new JLabel("First Name");
    fNamePicLbl = new JLabel(image);     // add icon to jlable
    lNameLbl = new JLabel("Last Name");
    lNamePicLbl = new JLabel();
    genderLbl = new JLabel("Gender");

    maleRb = new JRadioButton("Male");
    femaleRb = new JRadioButton("Female");
    temp3 = new JLabel();
    group = new ButtonGroup();
    group.add(maleRb);
    group.add(femaleRb);

    fNameTf = new JTextField(10);
    lNameTf = new JTextField(10);

    panel.add(fNameLbl);
    panel.add(fNameTf);
    panel.add(fNamePicLbl);
    panel.add(lNameLbl);
    panel.add(lNameTf);
    panel.add(lNamePicLbl);
    panel.add(genderLbl);
    JPanel radioPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    radioPanel.add(maleRb);
    radioPanel.add(femaleRb);
    panel.add(radioPanel);
    panel.add(temp3);
    panel.add(okBtn);
    okBtn.addActionListener(this);
    panel.add(cancelBtn);
    cancelBtn.addActionListener(this);
    panel.add(tempBtn);

    panel.setLayout(new SpringLayout());
    SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 10, 80, 60);
    return panel;
}

当我运行程序时,图标不显示。

最佳答案

Check.png 的路径似乎有问题。正确的路径和图标将出现。 如果图标存储在资源文件夹图标下,则路径应如下所示。

ImageIcon image = new ImageIcon("icons/Check.png");

关于java - 将图标设置为 JLabel - 不显示图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19400578/

相关文章:

c# - 比较两个位图图像以检查它们在 WPF 中是否不同的最快方法

php - 创建只有我的站点可以读取的图像文件格式

java - Map Reduce Eclipse 2.4.1 hadoop中的客户端jar

java - Spring boot JPA @Entity创建临时变量

java - 在 Selenium 中获取图像 src

java - ListSelectionListener 无法识别 if 条件中的联合

java - 在 Android 上录制音频和在 Swing (PC) 上播放时出现问题

java - 为什么哈希码不生成唯一的哈希码?

java - 过滤掉时间复杂度为 O(n) 的列表元素

java - 如何在 JScrollPane 中修改 JPanel 的大小?