“尚未创建缓冲区” ...同时创建缓冲区
我有(我认为是)一个简单的 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)
I have (what I thought was) a straightforward BufferStrategy for a JFrame. It is created like so:
// Buffer
container.createBufferStrategy(2);
strategy = container.getBufferStrategy();
However, occassionally I receive the following error (which points to the first line of the preceeding snippet as the offending item) :
java.lang.IllegalStateException: Buffers have not been created
This error is peculiar as it comes and goes - sometimes it is triggered, sometimes not. I suspect this means it's a threading issue. Does anyone have any pointers as to what might be going on here? I'm a little at a loss, since I'm already trying to do what Java says it wants me to do!
edit: full trace:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您调用
createBufferStrategy
时,框架需要可显示。另外,正如 camickr 所指出的,您需要从 EDT 中调用它。确保这一点的一种方法是扩展
JFrame
并覆盖addNotify
:The frame needs to be displayable when you call
createBufferStrategy
. Also as camickr has pointed out you need to call it from the EDT.One way to ensure this is to extend
JFrame
and overrideaddNotify
:Swing 组件默认情况下是双缓冲的,因此无需使用 BufferStrategy。
然而,当您遇到这样的随机错误时,通常是因为代码未在 EDT 上执行。有关详细信息,请阅读 Swing 教程中有关并发的部分。
Swing components are double buffered by default, so there is no need to play around with a BufferStrategy.
However when you get random errors like that its usually because code is not executed on the EDT. Read the section from the Swing tutorial on Concurrency for more information.