组件必须具有有效的对等方-BufferStrategy [英] Component must have a valid peer - BufferStrategy

查看:163
本文介绍了组件必须具有有效的对等方-BufferStrategy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道以前曾问过类似的问题,但是似乎没有答案可以解决我的问题.

First off all, I know questiones like this has been asked before, but no answers seem to fix my problem.

我正在做一个小游戏,由于某种原因,每当我尝试创建新的缓冲策略时,java都会返回IllegalStateException.我正在将游戏添加到JFrame中,但是仍然抛出异常,这是添加到JFrame中的代码:

I am working on a little game, and for some reason java returns an IllegalStateException whenever I try to create a new bufferstrategy. I am adding the game to a JFrame, but the exception is still thrown, here is the code for adding to the JFrame:

JFrame frame;


public Window(int x, int y, int width, int height, String title, boolean focus, Main game) throws IOException {
        frame = new JFrame();
        frame.setLocation(x, y);
        frame.setSize(new Dimension(width, height));
        frame.setTitle(title);
        frame.add(game);
        game.start();
        frame.setAutoRequestFocus(focus);
        frame.setFocusable(true);
        frame.setVisible(true);
    }

这是用于创建窗口的代码(位于Main类中):

And here is the code for creating the window (located in the Main class):

window = new Window(x, y, WIDTH, HEIGHT, "Title", true, this);

推荐答案

我假设在game.start()的框架上调用了createBufferStrategy().

I assume createBufferStrategy() is called on frame from game.start().

IllegalStateException可能会发生,因为在从JVM外部为其分配资源之前,JFrame并不真正存在于计算机中(或类似的东西).

The IllegalStateException may happen because the JFrame doesn't really exist in the computer (or something like that) until it has been assigned resources from outside the JVM.

当我自己尝试创建createBufferStrategy()时,错误提示组件必须具有有效的同级".显然,对等是图形组件的示例版本,操作系统或图形管理器将其用作绘制自定义组件的原型.

When I myself tried to createBufferStrategy(), the error said that "Component must have a valid peer". Peers, apparently, are example versions of graphical components that the OS or graphics manager uses as an archetype for drawing your custom components.

我猜想,直到在操作系统中为您的JFrame分配了它的对等方之前,它都不具备制作BufferStrategy所需的所有信息-JFrame的大小可能在内部以0乘0列出,也许是,除非您告诉JVM使Frame可显示或有效",否则它不会按高度进行宽度更新.您需要在调用game.start()之前执行此操作.

I guess that, until your JFrame is assigned its peer in the OS, it doesn't have all the information it needs to make a BufferStrategy--the size of the JFrame may be listed internally as 0 by 0, perhaps, and it won't be updated to width by height until you tell the JVM to make the Frame showable or "valid". You need to do this before calling game.start().

frame.setVisible(true)将显示该帧,并根据需要显然分配对等体.之后,您可以调用game.start().

frame.setVisible(true) will show the frame, and apparently assigns peers as necessary. You can call game.start() afterwards.

如果要在不可见的JFrame上调用createBufferStrategy(),请尝试使用frame.pack(),它会在不显示框架的情况下验证框架中的每个组件.注意:它还会压缩框架以适合其组件-如果您尚未添加任何东西,或者尚未调用setMinimumSize(),JFrame将会缩小.

If you want to call createBufferStrategy() on an invisible JFrame, try frame.pack(), which validates every component in the frame without showing it. Note: it also compress the frame to fit its components--if you haven't added anything, or haven't called setMinimumSize() yet, the JFrame will shrink.

这篇关于组件必须具有有效的对等方-BufferStrategy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆