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

有谁可以告诉我这个程序的问题,帮帮忙
import   javax.swing.*;
import   java.awt.*;
import   java.awt.event.*;
import   java.util.*;
public   class   图形界面入门   {
  public   static   void   main(String   argv[])
  {draw   p=new   draw();
  }
}

class   draw   extends   JFrame    
{
  public   draw()
    {setLayout(new   GridLayout(2,1));
      getContentPane().add(new   in1());
      getContentPane().add(new   in());
      Dimension   p=Toolkit.getDefaultToolkit().getScreenSize();
      int   x=p.width;
      int   y=p.height;
      setLocation((x-500)/2,(y-500)/2);
      setSize(500,500);
      setVisible(true);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    }
 

 
}

class   in   extends   JPanel    
{
  private   JButton   p;
  private   JButton   q;
  private   in1   s=new   in1();
  public   in()
  {p=new   JButton( "OK ");
    q=new   JButton( "Cancel ");
    add(p);
    add(q);
    p.addActionListener(s);
    q.addActionListener(s);  
   
  }


}

class   in1   extends   JPanel     implements   ActionListener
{private   Graphics   g;
  public   void   actionPerformed(ActionEvent   e)
    {g=getGraphics();
      super.paintComponent(g);
      if(e.getActionCommand().equals( "OK "))
      //总是下面这里出错
    g.drawString( "OK   is   clicked ",getWidth()/2,getHeight()/5);
      if(e.getActionCommand().equals( "Cancel "))
  //总是下面这里出错
    g.drawString( "Cancel   is   clicked ",getWidth()/2,getHeight()/5);
      g.dispose();
    }
 
}

这里是问题
Exception   in   thread   "AWT-EventQueue-0 "   java.lang.NullPointerException
at   in1.paintComponent(图形界面入门.java:61)
at   javax.swing.JComponent.paint(Unknown   Source)
at   javax.swing.JComponent.paintChildren(Unknown   Source)
at   javax.swing.JComponent.paint(Unknown   Source)
at   javax.swing.JComponent.paintChildren(Unknown   Source)
at   javax.swing.JComponent.paint(Unknown   Source)
at   javax.swing.JLayeredPane.paint(Unknown   Source)
at   javax.swing.JComponent.paintChildren(Unknown   Source)
at   javax.swing.JComponent.paintWithOffscreenBuffer(Unknown   Source)
at   javax.swing.JComponent.paintDoubleBuffered(Unknown   Source)
at   javax.swing.JComponent.paint(Unknown   Source)
at   java.awt.GraphicsCallback$PaintCallback.run(Unknown   Source)
at   sun.awt.SunGraphicsCallback.runOneComponent(Unknown   Source)
at   sun.awt.SunGraphicsCallback.runComponents(Unknown   Source)
at   java.awt.Container.paint(Unknown   Source)
at   sun.awt.RepaintArea.paintComponent(Unknown   Source)
at   sun.awt.RepaintArea.paint(Unknown   Source)