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

时间:2021-02-23 18:55:03

I would like to know about the error that I am getting. I am trying to set individual pixels on a jframe with the bufferedimage class, but for some reason when I try to add them to the frame, I get an error saying that no suitable method has been found.

我想知道我得到的错误。我试图用bufferedimage类在jframe上设置单个像素,但出于某种原因,当我尝试将它们添加到框架时,我得到一个错误,说没有找到合适的方法。

Here is my code and the error can someone please tell me how to add the bufferedimage to the frame please.

这是我的代码和错误可以有人请告诉我如何将bufferedimage添加到框架请。

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);
    }
}

Error:

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

2 个解决方案

#1


1  

You can pass the BufferedImage to an ImageIcon and then pass the ImageIcon to a JLabel. Finally, add this JLabel, which contains nothing but your image, as you would any other JLabel.

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

#2


1  

As already indicated by the error message JFrame#add is undefined for a non-component such as BufferedImage. You could do

正如错误消息JFrame所示,对于非组件(如BufferedImage),未定义add。你可以做到

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

#1


1  

You can pass the BufferedImage to an ImageIcon and then pass the ImageIcon to a JLabel. Finally, add this JLabel, which contains nothing but your image, as you would any other JLabel.

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

#2


1  

As already indicated by the error message JFrame#add is undefined for a non-component such as BufferedImage. You could do

正如错误消息JFrame所示,对于非组件(如BufferedImage),未定义add。你可以做到

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