删除frame.add(Component)时,组件必须是有效的对等方

boy*_*oyd 3 java swing bufferedimage awt

我这里有这个代码用于创建像素数组并将其绘制到图像中:

import javax.swing.JFrame;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

public class test extends Canvas implements Runnable {

    private static final long serialVersionUID = 1L;
    public static int WIDTH = 800;
    public static int HEIGHT = 600;
    public boolean running = true;
    public int[] pixels;
    public BufferedImage img;
    public static JFrame frame;
    private Thread thread;

    public static void main(String[] arg) {
        test wind = new test();
        frame = new JFrame("WINDOW");
        frame.add(wind);
        frame.setVisible(true);
        frame.setSize(WIDTH, HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        wind.init();
    }

    public void init() {
        thread = new Thread(this);
        thread.start();
        img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
    }

    public void run() {
        while (running) {
            render();
            try {
                thread.sleep(55);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void render() {
        BufferStrategy bs = this.getBufferStrategy();
        if (bs == null) {
            createBufferStrategy(4);
            return;
        }
        drawRect(0, 0, 150, 150);
        Graphics g = bs.getDrawGraphics();
        g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
        g.dispose();
        bs.show();

    }

    private void drawRect(int x, int y, int w, int h) {
        for (int i = x; i < w; i++) {
            for (int j = x; j < h; j++) {
                pixels[i + j * WIDTH] = 346346;
            }
        }


    }
}
Run Code Online (Sandbox Code Playgroud)

当我删除该行时,为什么会出现"组件必须是有效的对等"错误:

frame.add(wind);
Run Code Online (Sandbox Code Playgroud)

我为什么要删除它?因为我想使用类对象(来自另一个文件)创建一个框架,并使用代码Window myWindow = new Window()完成相同的事情.

tra*_*god 5

作为@nIcE cOw评论,您似乎是混合重量级和轻量级组件.替换Frame仍然留下了潜在的问题:createBufferStrategy()抛出异常,因为在Canvas添加之前它是不可显示的Frame,这依赖于主机平台提供的重量级对等组件的功能.实际上,您正在尝试选择a BufferStrategy而不指定哪个缓冲区应该使用该策略.

相反,使用现有的引擎,或者依赖于所提供的默认缓冲策略JComponent,为例子.

Exception in thread "Thread-2" java.lang.IllegalStateException: Component must have a valid peer
    at java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:3843)
    at java.awt.Component$FlipBufferStrategy.(Component.java:3817)
    at java.awt.Component$FlipSubRegionBufferStrategy.(Component.java:4358)
    at java.awt.Component.createBufferStrategy(Component.java:3699)
    at java.awt.Canvas.createBufferStrategy(Canvas.java:166)
    at java.awt.Component.createBufferStrategy(Component.java:3623)
    at java.awt.Canvas.createBufferStrategy(Canvas.java:141)
    at test.render(test.java:52)
    at test.run(test.java:40)
    at java.lang.Thread.run(Thread.java:680)