简体   繁体   English

在 mouseListener 中删除并添加新的 JLabel

[英]Remove and add new JLabel in mouseListener

I have 2 JLabels , and I add MouseListener to one of them,which can remove other JLabel and draw it again(when I click on one of them,the Image of other one change) this is my code,but it does't work,it remove JLabel ,but don't paint it again,what should I do?我有 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();
}

is there another way exist,which when I click on one of the JLabel ,it can change Image of other one?是否存在另一种方法,当我单击其中一个JLabel时,它可以更改另一个的Image

Call revalidate() before repaint()在 repaint() 之前调用 revalidate()

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

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