Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 错误:找不到合适的添加方法(BuffereImage)_Java_Swing_Bufferedimage_Pixels - Fatal编程技术网

Java 错误:找不到合适的添加方法(BuffereImage)

Java 错误:找不到合适的添加方法(BuffereImage),java,swing,bufferedimage,pixels,Java,Swing,Bufferedimage,Pixels,我想知道我得到的错误。我尝试使用BuffereImage类在jframe上设置单个像素,但由于某种原因,当我尝试将它们添加到帧时,我收到一个错误,表示没有找到合适的方法 这是我的代码和错误。有人能告诉我如何将BuffereImage添加到帧中吗 import javax.swing.JFrame; import java.awt.image.BufferedImage; public class gui { public static void main(String[] args) {

我想知道我得到的错误。我尝试使用BuffereImage类在jframe上设置单个像素,但由于某种原因,当我尝试将它们添加到帧时,我收到一个错误,表示没有找到合适的方法

这是我的代码和错误。有人能告诉我如何将BuffereImage添加到帧中吗

import javax.swing.JFrame;
import java.awt.image.BufferedImage;

public class gui {

  public static void main(String[] args) {
    int width = 40;
    int height = 80;
    int[] data = new int [width * height];
    JFrame frame = new JFrame("gui");
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    image.setRGB(0, 0, width, height, data, 0, width);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(image);
    frame.setSize(400, 400);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    }
}
错误:

gui.java:15: error: no suitable method found for add(BufferedImage)
    frame.add(image);
         ^
     method Container.add(Component,Object,int) is not applicable
      (actual and formal argument lists differ in length)
    method Container.add(Component,Object) is not applicable
      (actual and formal argument lists differ in length)
    method Container.add(Component,int) is not applicable
      (actual and formal argument lists differ in length)
    method Container.add(String,Component) is not applicable
      (actual and formal argument lists differ in length)
    method Container.add(Component) is not applicable
      (actual argument BufferedImage cannot be converted to Component by method invocation conversion)
    method Component.add(PopupMenu) is not applicable
      (actual argument BufferedImage cannot be converted to PopupMenu by method invocation conversion)
1 error

您可以将
BufferedImage
传递给
ImageIcon
,然后将
ImageIcon
传递给
JLabel
。最后,添加此
JLabel
,它只包含您的图像,就像您添加任何其他
JLabel

一样,正如错误消息所示
JFrame\add
对于非组件(如
BufferedImage
)是未定义的。你可以

frame.add(new JLabel(new ImageIcon(image)));

不是100%相关,但它应该有助于引导您找到答案:<代码>图像图标包装在
容器中
可能是另一个路由,但尚未对其进行测试。错误本身是因为,如上所述,没有方法接受该对象类型。