我的Java drawString命令无论出于何种原因都不能绘制文本

时间:2018-04-27 13:20:50

标签: java swing awt

此处显示的文字无论出于何种原因都不会出现在代码中。

它应该如下所示:

enter image description here

有问题的代码:

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
public class CRlab31 extends Canvas
{
  public static void main (String[] args)
  {
    JFrame w=new JFrame("Graphics");
    w.setSize(800,600);
    w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    CRlab31 canvas=new CRlab31();
    w.add(canvas);
    w.setVisible(true);
  }
  public void paint(Graphics g)
  {
    g.setColor(Color.green);
    g.drawRect(50,20,100,200);
    g.fillOval(160,20,100,200);
    g.setColor(Color.blue);
    g.fillRect(200,400,200,20);
    g.drawOval(200,430,200,100);
    g.setColor(Color.black);
    g.drawString("Graphics are pretty neat.",500,100);
    int x=getWidth()/2;
    int y=getHeight()/2;
    g.drawString("The first letter of this string is at ("+x+","+y+")",x,y);
  }
}

我在这里应该做些什么来使文本实际显示在示例中?

0 个答案:

没有答案