我试过这个,但它不工作。
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class Gfx
{
public static void main(String[] args)
{
Frame f = new Frame ("Gfx");
f.addWindowListener (new WindowAdapter() {
public void windowClosing(WindowEvent e) {
f.dispose (); }});
Panel p = new Panel ();
f.add(p);
BufferedImage image = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_ARGB);
p.add(image);
f.pack();
f.setVisible (true);
}
}
错误为:
Gfx.java:21: error: no suitable method found for add(BufferedImage)
p.add(image);
^
method Component.add(PopupMenu) is not applicable
(argument mismatch; BufferedImage cannot be converted to PopupMenu)
method Container.add(Component) is not applicable
(argument mismatch; BufferedImage cannot be converted to Component)
BufferedImage
不是Component
。如何将其转换为Component
?
2条答案
按热度按时间carvr3hs1#
如果你想使用AWT来绘图,你可以使用
Canvas
。在Java Swing中,你可以创建一个绘图JPanel
。我修改了你的代码来创建下面的GUI:
我缩小了图像大小,以便GUI更好地适应答案。
下面是完整的可运行代码。
7bsow1i62#
完整示例及部分图纸: