如何将相同的图标设置为 JLabel 数组

问题描述

我有 14 个 jLabel,它们需要在方法上设置相同的图标。而且我认为将它们放在 JLabel 数组中并设置相同的图标会更简化。但我找不到实现这一目标的方法,这是我的尝试:

    private final JLabel tarjetas[] = {this.jLabel1,this.jLabel2,this.jLabel3,this.jLabel4,this.jLabel5,this.jLabel6,this.jLabel7,this.jLabel8,this.jLabel9,this.jLabel10,this.jLabel11,this.jLabel14
};

public void iniciarReiniciar(){
    for (JLabel tarjeta : tarjetas) {
        tarjeta.setIcon(new ImageIcon(getClass().getResource("interrogationPoint.png")));
    }
    /* This is how setting the icons manually,it works,but I want to try it on a JLabel array
    jLabel1.setIcon(new ImageIcon(getClass().getResource("interrogationPoint.png")));
    jLabel2.setIcon(new ImageIcon(getClass().getResource("interrogationPoint.png")));
    jLabel3.setIcon(new ImageIcon(getClass().getResource("interrogationPoint.png")));
    jLabel4.setIcon(new ImageIcon(getClass().getResource("interrogationPoint.png")));
    jLabel5.setIcon(new ImageIcon(getClass().getResource("interrogationPoint.png")));
    jLabel6.setIcon(new ImageIcon(getClass().getResource("interrogationPoint.png")));
    jLabel7.setIcon(new ImageIcon(getClass().getResource("interrogationPoint.png")));
    jLabel8.setIcon(new ImageIcon(getClass().getResource("interrogationPoint.png")));
    jLabel9.setIcon(new ImageIcon(getClass().getResource("interrogationPoint.png")));
    jLabel10.setIcon(new ImageIcon(getClass().getResource("interrogationPoint.png")));
    jLabel11.setIcon(new ImageIcon(getClass().getResource("interrogationPoint.png")));
    jLabel12.setIcon(new ImageIcon(getClass().getResource("interrogationPoint.png")));
    jLabel13.setIcon(new ImageIcon(getClass().getResource("interrogationPoint.png")));
    jLabel14.setIcon(new ImageIcon(getClass().getResource("interrogationPoint.png")));
     */
}

这些是我的 JFrame 表单中的变量声明: Variables declaration

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)