繁体   English   中英

在 mouseListener 中删除并添加新的 JLabel

[英]Remove and add new JLabel in mouseListener

我有 2 个JLabels ,我将MouseListener添加到其中一个,它可以删除其他JLabel并再次绘制它(当我单击其中一个时,另一个更改的Image )这是我的代码,但它不起作用,它删除了JLabel ,但不要再次绘制它,我该怎么办?

ImageIcon icon_next = new ImageIcon("next-icon.PNG");
...
next = new JLabel(icon_next);
...
next.addMouseListener(this);
player_img_lbl = new JLabel(player_img_ico[0]);
add(player_img_lbl, FlowLayout.CENTER);
....
@Override
public void mouseClicked(MouseEvent e) {
        this.remove(player_img_lbl);
    JLabel player_img_lbl = new JLabel(player_img_ico[1]);
    add(player_img_lbl0,FlowLayout.CENTER);
    repaint();
}

是否存在另一种方法,当我单击其中一个JLabel时,它可以更改另一个的Image

在 repaint() 之前调用 revalidate()

暂无
暂无

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

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