Java Swing-在JPanel上重新绘制照片
嗨,伙计们,请你们帮帮我。 正确地说,我只需要用从文件中获取的不同照片刷新Java Swing-在JPanel上重新绘制照片,java,swing,jpanel,Java,Swing,Jpanel,嗨,伙计们,请你们帮帮我。 正确地说,我只需要用从文件中获取的不同照片刷新JPanel。 第一次添加带有相框照片的JPanel时-照片显示正确!一切都好 但是当我尝试用另一张照片动态更改当前照片并刷新JPanel-我看到的是相同的(旧)照片。 使用以下“刷新”部分代码的位置并不重要: picturePanel.repaint() picturePanel.validate() 您可以在下面找到代码: // create the own JPanel public class ImagePa
JPanel
。
第一次添加带有相框照片的JPanel
时-照片显示正确!一切都好
但是当我尝试用另一张照片动态更改当前照片并刷新JPanel
-我看到的是相同的(旧)照片。
使用以下“刷新”部分代码的位置并不重要:
picturePanel.repaint()
picturePanel.validate()
您可以在下面找到代码:
// create the own JPanel
public class ImagePanel extends JPanel {
private Image image;
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
@override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
} else
System.out.println("The Picture is Missing!");
}
}
从文件中获取照片并将其添加到自己的JPanel(ImagePanel)
以及JPanel的主要呼吁:
picturePanel=getTestPicture("picture.jpg");
frame.add(picturePanel); //looks Correct - Photo is visible.
....
如果我们试图在节目期间再次重新粉刷JPanel,旧照片会留在面板上。新照片没有上漆
picturePanel=getTestPicture("picture.jpg");
frame.add(picturePanel); //picture.jpg - it`s showed correctly!
picturePanel=getTestPicture("pic2.jpg");
picturePanel.repaint();
picturePanel.validate();
//doesn`t work ! picture.jpg is on the JPanel still !
请大家帮帮我!我需要了解我的代码中有什么错误!请不要建议使用JLabel或类似的东西
提前谢谢你 不要将新的
ImagePanel
添加到框架中,请更新现有的框架
public class SomeOtherComponent extends JPanel {
private ImagePanel imagePanel;
//...
public SomeOtherComponent() {
//...
imagePane = getTestPicture("picture.jpg");
add(imagePane);
//...
}
当您需要更改图像时,只需使用
imagePane.setImage(ImageIO.read(...));
imagePane.repaint();
imagePane.setImage(ImageIO.read(...));
imagePane.repaint();