import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Tmouse extends JApplet implements MouseListener {
//Container con;
public  Container con;
int width,height;
int x,y;
int flag;
String mouseStatu;
 //Graphics g;
public Graphics g;
public Tmouse()
{
con=this.getContentPane();
g=con.getGraphics();
}
public void init(){
/*setSize(200,100);
setVisible(true)*/;
//con=this.getContentPane();
x=0;
y=0;
width=2;
height=2;
flag=0;
//g= con.getGraphics();
con.addMouseListener(this);
//g= con.getGraphics();
setSize(200,100);
setVisible(true);
update(g);
}
public void update(Graphics g){
g.setColor(this.getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
paint(g);
}
public void paint(){
g.setColor(Color.blue);
g.drawString("Click the Mouse Button", 5, 20);
g.setColor(Color.red);
if(flag==1)
g.drawString("MouseEntered", 5, 80);
else if(flag==2)
g.drawString("MouseExited", 5, 80);
else if(flag==3)
{
g.drawString("MouseEntered", 5, 80);
g.fillRect(x, y, width, height);
g.drawString("click", x, y);
}
else if (flag==4)
{
g.drawString("MouseEntered", 5, 80);
g.fillRect(x, y, width, height);
g.drawString("press", x, y);

}
}public void mouseEntered(MouseEvent e)
{
flag=1;
repaint();
}
public void mouseExited(MouseEvent e){

flag=2;
repaint();
}
public void mouseClicked(MouseEvent e){
flag=3;
x=e.getX();
y=e.getY();
repaint();
}
public void mousePressed(MouseEvent e){
flag=4;
x=e.getX();
y=e.getY();
repaint();

}
public void mouseReleased(MouseEvent e){
flag=5;
x=e.getX();
y=e.getY();
repaint();

}
public static void main(String[] args) {
Tmouse t=new Tmouse();
t.init();

   
}}
老是报错说我指针异常出现问题的地方在 con=this.getContentPane();t.init();update(g);
这3个地方请高手指教