import javax.swing.*;
import java.awt.*;public class NotHelloWorld{
public static void main(String args[]){
NotHelloWorldFrame frame=new NotHelloWorldFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
} class NotHelloWorldFrame extends JFrame
 {
  public NotHelloWorldFrame()
  {
  setTitle("lzy");
  setSize(DEFAULT_WINTH,DEFAULT_HEIGHT);
  NotHelloWorldPanel panel=new NotHelloWorldPanel();
  Container contentPane=getContentPane();
  contentPane.add(panel);
  }
 
  public static final int DEFAULT_WINTH=300;
  public static final int DEFAULT_HEIGHT=200;
 }
 
 class NotHelloWorldPanel extends JPanel
 {
  public void paintCompontnt(Graphics g)
  {
  super.paintComponent(g);
 
  g.drawString("I love you!",MESSAGE_X,MESSAGE_Y);
 
  }
  public static final int MESSAGE_X=75;
  public static final int MESSAGE_Y=100;
 }

解决方案 »

  1.   


    public void paintCompontnt(Graphics g){}
    改为
    public void paint(Graphics g) {}
      

  2.   

    你方法使用有误
    import javax.swing.*;
    import java.awt.*; class NotHelloWorld{
    public static void main(String args[]){
    NotHelloWorldFrame frame=new NotHelloWorldFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
    }
    } class NotHelloWorldFrame extends JFrame
     {
      public NotHelloWorldFrame()
      {
      setTitle("lzy");
      setSize(DEFAULT_WINTH,DEFAULT_HEIGHT);
      NotHelloWorldPanel panel=new NotHelloWorldPanel();
      Container contentPane=getContentPane();
      contentPane.add(panel);
      }
     
      public static final int DEFAULT_WINTH=300;
      public static final int DEFAULT_HEIGHT=200;
     }
     
     class NotHelloWorldPanel extends JPanel
     {
      public void paint(Graphics g)
      {
      super.paint(g);
     
      g.drawString("I love you!",MESSAGE_X,MESSAGE_Y);
     
      }
      public static final int MESSAGE_X=75;
      public static final int MESSAGE_Y=100;
     }
      

  3.   

    修改成这样public void paint(Graphics g)
      

  4.   

    为什么这么改??明明我的字符串是写在panel上的阿??