繁体   English   中英

如何设置列表中的所有JLabel以更改显示的内容

[英]How do you set all JLabels made in a list to change what is shown

固定

很抱歉,我的头衔不好,但是我花了5分钟在上面,不能说出更好的对不起。

我需要它,以便如果您全部按下JButton,它将把图像全部放入2,并将其放入JLabel标签(由列表制成)

我将JLabel all2放在map()上方,因为如果不这样做,“ ImageIcon setAll无法解析”。 我没有将JLabel标签放在map()上方,因为它与创建由listofLabels组成的100个JLable混淆了。 它只会显示一个标签。 在此处输入图片说明在此处输入图片说明

public class mapMaker {

ArrayList<JLabel> listofLabels = new ArrayList<JLabel>(100);
ImageIcon forest = new ImageIcon("resources/terrains/forest.jpg");
ImageIcon wood = new ImageIcon("resources/terrains/wood.jpg");

JFrame frame = new JFrame("D&D");
JLabel all2=new JLabel( wood);



public map() {
    int a=0,b=50;
    JFrame.setDefaultLookAndFeelDecorated(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(100,0,1000,700);
    frame.getContentPane().setLayout(null);
    frame.setVisible(true);


    JButton all=new JButton("Set All To");
    frame.getContentPane().add(all);
    all.setBounds(600,450,150,50);
    all.setFont(new Font("Courior", Font.BOLD, 25));
    all.addActionListener(boardListener);

    frame.getContentPane().add(all2);
    all2.setBounds(800,450,50,50);
     all.addActionListener(boardListener);


for ( i = 0; i < 100; i++) {
    JLabel label =new JLabel(forest);    
    label.setIcon(forest);
        listofLabels.add(label);
        a=a+50;
        if(a>549) {
            b=b+50;
            a=50;
        }
        frame.getContentPane().add(label);
        label.setBounds(a, b, 50,50);
        label.setTransferHandler(new TransferHandler("icon"));


            }
}


ActionListener boardListener = new ActionListener (){
    public void actionPerformed(ActionEvent e){
ImageIcon setAll=(ImageIcon) all2.getIcon();

![enter image description here][2]label.setIcon(setAll);



    }
    };;

public static void main(String[]args) {
    new map();
}

}

为什么不简单地遍历JLabel的数组(或ArrayList),将所有JLabel的图标设置为所选的图标呢?

例如:

ActionListener boardListener = new ActionListener() {
  public void actionPerformed(ActionEvent e) {
     if (e.getActionCommand().equals("Set All To")) {
        ImageIcon setAll = (ImageIcon) all2.getIcon();
        for (JLabel label : listofLabels) {
           label.setIcon(setAll);
        }
     }
  }
};

我不确定这是您所期望的:

ActionListener boardListener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Set All To")) {
          Icon setAllIcon = all2.getIcon();

          for (JLabel label : listofLabels)
            label.setIcon(setAllIcon);
        }
    }
};;

已进行编辑,因为它可以正常工作,因为其中提到的代码中存在一些错误:

代替这个:

all.addActionListener(boardListener);

frame.getContentPane().add(all2);
all2.setBounds(800,450,50,50);
 all.addActionListener(boardListener);

看起来您想执行以下操作:

all.addActionListener(boardListener);

frame.getContentPane().add(all2);
all2.setBounds(800,450,50,50);
all2.addActionListener(boardListener); // This is the changed line

暂无
暂无

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

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