【问题标题】:JFrame does not refresh after deleting an image删除图像后JFrame不刷新
【发布时间】:2011-02-20 20:08:37
【问题描述】:

我第一次在 JFrame 中处理图像,但遇到了一些问题。我成功地将图像放在我的 JFrame 上,现在我想在 2 秒后从 JFrame 中删除我的图像。但是 2 秒后,图像不会消失,除非我调整框架的大小或者我最小化然后最大化框架。如果可以的话,请帮助我。谢谢。

代码如下:

        File f = new File("2.jpg");

System.out.println("图片" + f.getAbsolutePath()); BufferedImage 图像 = ImageIO.read(f); MyBufferedImage img = new MyBufferedImage(image); img.resize(400, 300); img.setSize(400, 300); img.setLocation(50, 50); getContentPane().add(img);

this.setSize(600, 400); this.setLocationRelativeTo(null); this.setVisible(true);

Thread.sleep(2000); System.out.println("2秒结束");

getContentPane().remove(img);

这是 MyBufferedImage 类:

public class MyBufferedImage extends JComponent{
 private BufferedImage image;

private int nPaint;
private int avgTime;

private long previousSecondsTime;

public MyBufferedImage(BufferedImage b) {
    super();

    this.image = b;

    this.nPaint = 0;
    this.avgTime = 0;

    this.previousSecondsTime = System.currentTimeMillis();
}

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    g2D.setColor(Color.BLACK);
    g2D.fillRect(0, 0, this.getWidth(), this.getHeight());

    long currentTimeA = System.currentTimeMillis();


    //g2D.drawImage(this.image, 320, 0, 0, 240, 0, 0, 640, 480, null);
    g2D.drawImage(image, 0,0, null);
    long currentTimeB = System.currentTimeMillis();
    this.avgTime += currentTimeB - currentTimeA;
    this.nPaint++;

    if (currentTimeB - this.previousSecondsTime > 1000) {
        System.out.format("Drawn FPS: %d\n", nPaint++);
        System.out.format("Average time of drawings in the last sec.: %.1f ms\n", (double) this.avgTime / this.nPaint++);
        this.previousSecondsTime = currentTimeB;
        this.avgTime = 0;
        this.nPaint = 0;
    }
}

}

【问题讨论】:

    标签: java image jframe bufferedimage


    【解决方案1】:

    删除图像后只需致电this.repaint(),一切都会好起来的;)

    【讨论】:

      【解决方案2】:

      您可能需要使框架组件无效以强制重绘。

      您最好的选择可能是查看更新/重绘方法。

      【讨论】:

        【解决方案3】:

        你有没有试过打电话

        getContentPane().revalidate() ;

        调用删除后?

        【讨论】:

        • 没有revalidate方法,尝试使用getContentPane().validate();或者可能(在 JFrame 上)执行 this.validateTree();
        • 噢!此外,他们希望注意在 EDT 中添加和删除内容
        【解决方案4】:

        您应该确保从事件调度线程中的组件中删除图像。试试这个:

         SwingUtilities.invokeLater(new Runnable() {
                 public void run() {
                      getContentPane().remove(img);
                 }
         }
        

        您的img 需要是全局的或在本地范围内声明的final 才能工作。如果您还不熟悉,请查看concepts on Swing Threads

        注意:如果内容窗格被认为有效,Container 上的 remove 调用将调用 invalidate()

        【讨论】:

          【解决方案5】:

          SwingUtilities.updateComponentTreeUI(this);一枪

          【讨论】:

          • 非常感谢,现在可以了。它适用于 SwingUtilities.updateComponentTreeUI(this)、this.repaint 或 this.update(getGraphics())。祝你有美好的一天!
          猜你喜欢
          • 1970-01-01
          • 2011-06-02
          • 2012-09-28
          • 1970-01-01
          • 1970-01-01
          • 2015-11-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多