用eclipse写的。用了JPanel类的drawString编译后却显示不出来相应的内容

来源:百度知道 编辑:UC知道 时间:2024/06/15 16:33:22
//代码如下,编译后显示不出来hello,不知道是什么原因

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

public class newidone
{
public static void main(String[] args)
{
AllFrame frame=new AllFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

class AllFrame extends JFrame
{
public AllFrame()
{
setTitle("newidone");
setSize(500,500);

DrawPaint draw=new DrawPaint();
add(draw);
}
}

class DrawPaint extends JPanel
{
public void paintComonent(Graphics g)
{
g.drawString("hello",75,75);
}
}

DrawPaint类的paintComonent方法名错了!!应该是paint。
paintComonent方法是不会自动调用的!!

public void paintComonent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g; //添加这一句
g2.drawString("hello",75,75); //这里用g2调用
}