gpt4 book ai didi

java - "Buffers have not been created"... 同时创建缓冲区

转载 作者:搜寻专家 更新时间:2023-10-31 20:16:04 25 4
gpt4 key购买 nike

我有(我认为是)一个简单的 JFrame BufferStrategy。它是这样创建的:

    // Buffer
container.createBufferStrategy(2);
strategy = container.getBufferStrategy();

但是,我偶尔会收到以下错误(指向前面代码段的第一行作为违规项):

java.lang.IllegalStateException: 尚未创建缓冲区

这个错误来来去去很奇特 - 有时会触发,有时不会。我怀疑这意味着这是一个线程问题。有没有人对这里可能发生的事情有任何指示?我有点不知所措,因为我已经在尝试做 Java 要求我做的事情了!

编辑:完整跟踪:

Exception in thread "main" java.lang.IllegalStateException: Buffers have not been created
at sun.awt.windows.WComponentPeer.getBackBuffer(WComponentPeer.java:877)
at java.awt.Component$FlipBufferStrategy.getBackBuffer(Component.java:3815)
at java.awt.Component$FlipBufferStrategy.updateInternalBuffers(Component.java:3800)
at java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:3791)
at java.awt.Component$FlipBufferStrategy.<init>(Component.java:3730)
at java.awt.Component$FlipSubRegionBufferStrategy.<init>(Component.java:4253)
at java.awt.Component.createBufferStrategy(Component.java:3612)
at java.awt.Window.createBufferStrategy(Window.java:3015)
at java.awt.Component.createBufferStrategy(Component.java:3536)
at java.awt.Window.createBufferStrategy(Window.java:2990)

最佳答案

当您调用 createBufferStrategy 时,框架需要可显示。同样正如 camickr 指出的那样,您需要从 EDT 调用它。

确保这一点的一种方法是扩展 JFrame 并覆盖 addNotify:

class MyFrame extends JFrame {
public void addNotify() {
super.addNotify();
// Buffer
createBufferStrategy(2);
strategy = getBufferStrategy();
}
}

关于java - "Buffers have not been created"... 同时创建缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3435994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com