为何能够显示窗口却不能显示文字?

olive_hl 2007-09-29 12:16:07
代码如下,在 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
...全文
74 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jk88811 2007-09-29
  • 打赏
  • 举报
回复
public void Paint ( Graphics g )

Paint 改为 paint

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

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧