我想知道我得到的错误。我试图用bufferedimage类在jframe上设置单个像素,但出于某种原因,当我尝试将它们添加到框架时,我得到一个错误,说没有找到合适的方法。
这是我的代码,错误可以有人请告诉我如何将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);
}
}
错误:
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
答案 0 :(得分:1)
您可以将BufferedImage
传递给ImageIcon
,然后将ImageIcon
传递给JLabel
。最后,添加此JLabel
,其中只包含您的图片,与其他任何JLabel
一样。
答案 1 :(得分:1)
正如已经指出的错误消息JFrame#add
未定义的非组件,例如BufferedImage
。你可以做到
frame.add(new JLabel(new ImageIcon(image)));