新人菜鸟求助!

chiruchiru 2011-03-12 12:17:24

我定义了框架 也定义了面板 为什么只显示框架,没显示Hello,World 呢?

import javax.swing.*;
import java.awt.*;

public class HelloWorld
{
public static void main(String[] args)
{
TextFrame frame=new TextFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}
}

class TextFrame extends JFrame
{
public TextFrame()
{
setTitle("HelloWorld");
setSize(WIDTH,HEIGHT);

TextPanel panel=new TextPanel();
Container contenPane=getContentPane();
contenPane.add(panel);

}
public static final int WIDTH=300;
public static final int HEIGHT=200;
}

class TextPanel extends JPanel
{
public void painComponent(Graphics g)
{
super.paintComponent(g);

g.drawString("Hello,World", x, y);
}
public int x=100;
public int y=100;
}

...全文
54 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
AldisZhan 2011-03-12
  • 打赏
  • 举报
回复

原来有2个HelloWrold啊 晕!!
pengranxiang 2011-03-12
  • 打赏
  • 举报
回复
覆盖JPanel 的 painComponent() 方法写错了。 少了个 “t”, 并没覆盖Jpanel的 paintComponent()。 改成 paintComponent() 就好了。
AldisZhan 2011-03-12
  • 打赏
  • 举报
回复

setTitle("HelloWorld"); //设置了窗体的标题栏 HelloWorld 将会显示在 左上角 Java图标旁边
chiruchiru 2011-03-12
  • 打赏
  • 举报
回复
啊 少了个t 真的太马虎了 谢谢你了 分数是你的

62,614

社区成员

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

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