开发者

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 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:

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. You need to "add(...)" the label to the GUI in order for the label to be seen.

Read the Swing tutorial on How to Use Icons for working examples.

If you want to update an existing label on the GUI then you would use:

label.setIcon(...);

If you need more help post your SSCCE showing the problem.


The label you pass is not added to the GUI, the following method might help-

Add another JPanel as a container of jLabel1 and only replace its content:

i.e.:

    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:

  1. set the properties of jLabel1 as the properties of shopLabel, something like this.jLabel1.setText(shopLabel.getText());

  2. OR- remove jLabel1, add shopLabel and set this.jLabel1 to 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. The question is what are you doing with jLabel1 after its value has changed? 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.

To fix this you'll need to remove the old label instance and put in the new one. you could make this easier by using a container (JPanel) that old holds your Label. Then all you need to do is swap labels from the holder.


The only thing the GUI editor generated was 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. Now compare src/components/LabelDemo.java to your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜