日期:2014-05-20  浏览次数:20800 次

为何能够显示窗口却不能显示文字?
代码如下,在 JCreator 中编译没有问题,运行时候是弹出了窗口,标题栏和"Using colors"是正常输出了,可是代码中画的矩形和输出的文字为什么都没有了呢?就只有一个灰色的背景而已,请高手赐教……


//Demonstrating colors

//Java core packages
import java.awt.*;
import java.awt.event.*;
 
 
 //Java extension packages
import javax.swing.*;

public class ShowColors extends JFrame {
   
  //constructor sets windows's title bar string and dimensions
  public ShowColors() {
  super ( "Using colors" );
 
  setSize ( 400, 130 );
  setVisible ( true );
  }
   
  //draw rectangles and Strings in different colors
  public void Paint ( Graphics g )
  {
  //call supercalss's paint method
  super.paint ( g );
 
  //Set new drawing colors
  g.setColor ( new Color ( 0, 0, 0 ) );
  g.fillRect ( 25, 25, 100, 20 );
  g.drawString ( "Current RGB: ", 130, 40);
 
  g.setColor ( new Color ( 0.0f, 1.0f, 0.0f ) );
  g.fillRect ( 25, 50, 100, 20 );
  g.drawString ( "Current RGB: " + g.getColor (), 130, 65);
 
  g.setColor ( Color.blue );
  g.fillRect ( 25, 75, 100, 20 );
  g.drawString ( "Current RGB: " + g.getColor (), 130, 90);
 
  Color color = Color.magenta;
  g.setColor ( color );
  g.fillRect ( 25, 100, 100, 20 );
  g.drawString ( "Current RGB: " + g.getColor (), 130, 115);
  }
   
  //execute application
  public static void main(String[] args) {
  ShowColors application = new ShowColors();
  application.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
  }
}//end class ShowColors

------解决方案--------------------
public void Paint ( Graphics g )

Paint 改为 paint

区分大小写, 子类覆盖JFrame的paint方法, 实现在上面绘制的功能.