把addWindowListener(new MyWindowAdapter(this));
addMouseMotionListener(new MyMouseMotionAdapter(this));
换成this.addWindowListener(new MyWindowAdapter());
this.addMouseMotionListener(new MyMouseMotionAdapter());

解决方案 »

  1.   

    关闭窗口
        temp.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent evt) {
            System.exit(0);
          }
        });
      

  2.   

    import java.awt.*;
    import java.awt.event.*;
    class MyFrame extends Frame
    {
    public String msg = "have a good time";
    MousePanel mousePanel;
    public MyFrame()
    {
    addWindowListener(new MyWindowAdapter(this));
    mousePanel = new MousePanel();
    mousePanel.addMouseMotionListener(new MyMouseMotionAdapter(this));
    add(mousePanel);
    }class MousePanel extends Panel
    {
       public MousePanel()
       {      }
       public void paint(Graphics g)
       {
         g.drawString(msg,10,10);
         g.drawString("have a good time",20,20);
       }
         
    }public static void main(String argc[])
    {
    MyFrame temp = new MyFrame();
    temp.setSize(new Dimension(300,300));
    temp.setTitle("hava a good time");
    temp.setVisible(true);
    }
    }class MyWindowAdapter extends WindowAdapter
    {
    MyFrame myframe;
    public MyWindowAdapter(MyFrame temp)
    {
    myframe = temp;
    }
    public void windowClosing(WindowEvent event)
    {
    //myframe.setVisible(false);
    System.exit(0);
    }
    }
     class MyMouseMotionAdapter extends MouseMotionAdapter
    {
    MyFrame myframe;
    MyMouseMotionAdapter(MyFrame tempFrame)
    {
    this.myframe = tempFrame;
    }
    public void mouseMoved(MouseEvent me)
    {
    this.myframe.msg = "x is " + me.getX() + "y is " + me.getY();
    this.myframe.mousePanel.repaint();
    }
    }