📅  最后修改于: 2020-09-29 00:42:21             🧑  作者: Mango
Canvas控件代表一个空白的矩形区域,应用程序可以在其中绘制或捕获来自用户的输入事件。它继承了Component类。
public class Canvas extends Component implements Accessible
import java.awt.*;
public class CanvasExample
{
public CanvasExample()
{
Frame f= new Frame("Canvas Example");
f.add(new MyCanvas());
f.setLayout(null);
f.setSize(400, 400);
f.setVisible(true);
}
public static void main(String args[])
{
new CanvasExample();
}
}
class MyCanvas extends Canvas
{
public MyCanvas() {
setBackground (Color.GRAY);
setSize(300, 200);
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(75, 75, 150, 75);
}
}
输出: