简体   繁体   English

设置 JLabel 以显示图像的正确方法是什么?

[英]What is the proper way to set a JLabel to show an image?

Below is a snippet where I create my ImageIcon and JLabel:下面是我创建 ImageIcon 和 JLabel 的片段:

ImageIcon armorShopIcon = new ImageIcon("/images/armorshop.png", "Armor Shop");
JLabel armorShopLabel = new JLabel("Armor Shop", armorShopIcon, JLabel.CENTER);

object.setArmorImage(armorShopLabel);

Then in my object class I have a setter:然后在我的 object class 我有一个二传手:

public void setArmorImage(JLabel label) {
    this.jLabel1 = label;
}

This doesn't show the image when I test my application and I was wondering if someone could point out my mistake当我测试我的应用程序时,这没有显示图像,我想知道是否有人可以指出我的错误

EDIT编辑

Most of the source code:大部分源代码:

Main:主要的:

public class Main extends javax.swing.JFrame {

    public Main() {

    initComponents();

    ImageIcon armorIcon = new ImageIcon("/images/armorshop.png", "Armor Shop");
    JLabel armorShopLabel = new JLabel("Armor Shop", armorShopIcon, JLabel.CENTER);

    ShopDisplay armorShop = new ShopDisplay();
    armorShop.setArmorImage(armorShopLabel);


        public initComponents() { /*more generated code here*/ }

    }
}

Display:展示:

public class ShopDisplay extends javax.swing.JPanel {

    /** Creates new form ShopDisplay */
    public ShopDisplay() {
        initComponents();
    } 


    //generated by the gui builder
    //initComponents
    private void initComponents() {
    jLabel1 = new javax.swing.JLabel();
    jLabel1.setText("Shop Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(jLabel1, gridBagConstraints);
    }

    //Variable declarationg
    private javax.swing.JLabel jLabel1;


    //Setter for Shop Name
    public void setArmorImage(JLabel shopLabel) {
        this.jLabel1 = shopLabel;
    }

    //other setters for
    //multiple jLabels

}
public void setArmorImage(JLabel label)
{     
    this.jLabel1 = label; 
}

That code does nothing.该代码什么也不做。 Assigning a label reference to another variable does not affect the GUI.将 label 引用分配给另一个变量不会影响 GUI。 You need to "add(...)" the label to the GUI in order for the label to be seen.您需要将 label “添加(...)”到 GUI 才能看到 label。

Read the Swing tutorial on How to Use Icons for working examples.阅读有关如何使用图标的 Swing 教程以获取工作示例。

If you want to update an existing label on the GUI then you would use:如果您想在 GUI 上更新现有的 label,那么您可以使用:

label.setIcon(...);

If you need more help post your SSCCE showing the problem.如果您需要更多帮助,请发布您的SSCCE 以显示问题。

The label you pass is not added to the GUI, the following method might help-您传递的 label 未添加到 GUI 中,以下方法可能会有所帮助-

Add another JPanel as a container of jLabel1 and only replace its content:添加另一个 JPanel 作为jLabel1的容器并仅替换其内容:

ie: IE:

    private javax.swing.JPanel containerPanel;
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jLabel1.setText("Shop Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        containerPanel = new javax.swing.JPanel();
        containerPanel.add(jLabel1);
        add(containerPanel, gridBagConstraints);
    }

    //Variable declarationg
    private javax.swing.JLabel jLabel1;


    //Setter for Shop Name
    public void setArmorImage(JLabel shopLabel) {
        containerPanel.remove(jLabel1);
        containerPanel.add(shopLabel);
        jLabel1 = shopLabel;
        revalidate();
    }

, if you want to add it to the gui you can do one of the following: ,如果要将其添加到 gui 中,可以执行以下操作之一:

  1. set the properties of jLabel1 as the properties of shopLabel , something like this.jLabel1.setText(shopLabel.getText());将 jLabel1 的属性设置为jLabel1的属性,例如shopLabel this.jLabel1.setText(shopLabel.getText());

  2. OR- remove jLabel1 , add shopLabel and set this.jLabel1 to shopLabel :或 - 删除jLabel1 ,添加shopLabel并将this.jLabel1设置为shopLabel

For example:例如:

 public void setArmorImage(JLabel shopLabel) { remove(jLabel1); add(shopLabel); jLabel1 = shopLabel; revalidate(); }

You're creating a JLabel and then passing it to setArmorImage , which then assigns it to jLabel1 .您正在创建一个JLabel ,然后将其传递给setArmorImage ,然后将其分配给jLabel1 The question is what are you doing with jLabel1 after its value has changed?问题是在jLabel1的值改变后你在做什么? My guess is that you're putting jLabel1 in your container somewhere, then setArmorImage is called changing what jLabel1 points to, but the old label instance (previously pointed to by jLabel1 ) is still in your container.我的猜测是您将jLabel1放在容器中的某个位置,然后调用setArmorImage更改jLabel1指向的内容,但是旧的 label 实例(之前由jLabel1 )仍在您的容器中。

To fix this you'll need to remove the old label instance and put in the new one.要解决此问题,您需要删除旧的 label 实例并放入新实例。 you could make this easier by using a container ( JPanel ) that old holds your Label.您可以通过使用旧的容器 ( JPanel ) 来简化此操作,该容器 (JPanel) 包含您的 Label。 Then all you need to do is swap labels from the holder.然后,您需要做的就是从支架上交换标签。

The only thing the GUI editor generated was jLabel1 . GUI 编辑器生成的唯一东西是jLabel1

What no code in the fold?什么没有代码? Since no one else can see your computer, start from a working example that everyone can see.由于没有其他人可以看到您的计算机,因此请从每个人都可以看到的工作示例开始。 Unzip it, and type ant run .解压缩,然后输入ant run Now compare src/components/LabelDemo.java to your code.现在将src/components/LabelDemo.java与您的代码进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM