我有一个JLabel的图标,我只能看到一次更改。如果为空,则下面代码的新设置图像可以正常工作。但在那之后,图像被卡住了。没有新的图像可以取代它。当我在panelPainting上使用repaint而没有revalidate()时,我根本没有得到任何图片。多数民众赞成也很奇怪。
这是代码,(panelMain house panelPainting)
//get image from somewhere
JLabel imageLabel = new JLabel();
Icon imageIcon = new ImageIcon(image);
imageLabel.setIcon(imageIcon);
panelPainting.setAlignmentX(JLabel.CENTER);
panelPainting.add(imageLabel); // default center section
//my insanity starts here
panelPainting.revalidate();
panelMain.remove(panelPainting);
panelMain.revalidate();
编辑:我仔细检查了图片每次都发生了变化。
答案 0 :(得分:4)
使用JLabel.setIcon()
作为标准方式,然后没有理由在运行时删除,修改和添加新的JComponents
在某些情况下,在Icon
(来自外部来源,www网站等)重新绘制JLabel
时会出现问题,然后您必须致电,
myIcon.getImage().flush(); myLabel.setIcon(myIcon);
,否则
完成所有更改后,必须拨打container.revalidate()
和container.repaint()
作为最后一行代码,
更快地发布SSCCE,简短,可运行,可编辑,只需JLabel与JLabel包含ImageIcon / Icon created on fly