首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

!如何总是出错?大家帮小弟我看下那里出异常

2012-01-18 
救命啊!怎么总是出错??!大家帮我看下那里出错误!packageframeimportjava.awt.*importjava.awt.event.*i

救命啊!怎么总是出错??!大家帮我看下那里出错误!
package   frame;

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

public   class   MyApplet   extends   Applet   {
    private   boolean   isStandalone   =   false;
    BorderLayout   borderLayout1   =   new   BorderLayout();
    JPanel   command   =   new   JPanel();
    JPanel   workArea   =   new   JPanel();
    JPanel   parameter   =   new   JPanel();
    JButton   jButton1   =   new   JButton();
    JButton   line   =   new   JButton();

    Operation   operation   =   null;

    Canvas   image;
    Point   startPoint,   endPoint;
    int   mouse;

    //Get   a   parameter   value
    public   String   getParameter(String   key,   String   def)   {
        return   isStandalone   ?   System.getProperty(key,   def)   :
            (getParameter(key)   !=   null   ?   getParameter(key)   :   def);
    }

    //Construct   the   applet
    public   MyApplet()   {
    }

    //Initialize   the   applet
    public   void   init()   {
        startPoint   =   new   Point(-1,-1);
        try   {
            jbInit();
        }
        catch(Exception   e)   {
            e.printStackTrace();
        }
    }

    //Component   initialization
    private   void   jbInit()   throws   Exception   {
        this.setLayout(borderLayout1);
        workArea.setBackground(Color.white);
        workArea.addMouseMotionListener(new   MyApplet_workArea_mouseMotionAdapter(this));
        workArea.addMouseListener(new   MyApplet_workArea_mouseAdapter(this));
        //workArea.addMouseMotionListener(new   MyApplet_workArea_mouseMotionAdapter(this));
        jButton1.setText( "jButton1 ");
        line.setText( "墙壁 ");
        line.addActionListener(new   MyApplet_line_actionAdapter(this));
        this.add(command,     BorderLayout.NORTH);
        command.add(line,   null);
        this.add(workArea,   BorderLayout.CENTER);
        this.add(parameter,     BorderLayout.EAST);
        parameter.add(jButton1,   null);
    }

    //Get   Applet   information
    public   String   getAppletInfo()   {
        return   "Applet   Information ";
    }

    //Get   parameter   info


    public   String[][]   getParameterInfo()   {
        return   null;
    }

    void   line_actionPerformed(ActionEvent   e)   {
        mouse   =   1;
        operation   =   new   Operation(mouse);
    }

    void   workArea_mousePressed(MouseEvent   e)   {
        startPoint   =   new   Point(e.getX(),   e.getY());
       
    }

    void   workArea_mouseDragged(MouseEvent   e){
        if(startPoint.x!=-1&&startPoint.y!=-1   ){
            image   =   operation.choose();
        }
    }
    void   workArea_mouseReleased(MouseEvent   e)   {
        endPoint   =   new   Point(e.getX(),   e.getY());
        operation   =   new   Operation(mouse);
        operation.date(startPoint,   endPoint);
        workArea.add(image);
    }


}

class   MyApplet_line_actionAdapter   implements   java.awt.event.ActionListener   {
    MyApplet   adaptee;

    MyApplet_line_actionAdapter(MyApplet   adaptee)   {
        this.adaptee   =   adaptee;
    }
    public   void   actionPerformed(ActionEvent   e)   {
        adaptee.line_actionPerformed(e);
    }
}

class   MyApplet_workArea_mouseAdapter   extends   java.awt.event.MouseAdapter   {
    MyApplet   adaptee;

    MyApplet_workArea_mouseAdapter(MyApplet   adaptee)   {
        this.adaptee   =   adaptee;
    }
    public   void   mousePressed(MouseEvent   e)   {
        adaptee.workArea_mousePressed(e);
    }
    public   void   mouseReleased(MouseEvent   e)   {
        adaptee.workArea_mouseReleased(e);
    }
}

class   MyApplet_workArea_mouseMotionAdapter   extends   java.awt.event.MouseMotionAdapter   {
    MyApplet   adaptee;

    MyApplet_workArea_mouseMotionAdapter(MyApplet   adaptee)   {
        this.adaptee   =   adaptee;
    }
    public   void   mouseDragged(MouseEvent   e)   {
        adaptee.workArea_mouseDragged(e);
    }
}
辅助类:
package   frame;

import   java.awt.*;

public   class   Operation   {
    Process   process   =   null;

    Canvas   image;
    Point   startPoint,   endPoint;
    int   mouse;

    public   Operation(int   mouse)   {
        this.mouse   =   mouse;
    }

    void   date(Point   startPoint,   Point   endPoint)   {


        process   =   new   Process(startPoint,   endPoint);
        this.startPoint   =   process.startPoint;
        this.endPoint   =   process.endPoint;
    }

    Canvas   choose()   {
        switch   (mouse)   {
            case   1:
                image   =   new   Line(startPoint,   endPoint);
                return   image;
                //break;
            default:
                return   image;
        }
    }
}
直线类:
package   frame;

import   java.awt.*;

public   class   Line   extends   Canvas{
    Point   startPoint,   endPoint;
    public   Line(Point   startPoint,   Point   endPoint)   {
        this.startPoint   =   startPoint;
        this.endPoint   =   endPoint;
    }

    void   upDate(Graphics   g)   {
        g.setColor(Color.blue);
        g.drawLine(startPoint.x,   startPoint.y,   endPoint.x,   endPoint.y);
    }
}
辅助类:
package   frame;

import   java.awt.*;

public   class   Process   {
    Point   startPoint,   endPoint;

    public   Process(Point   startPoint,   Point   endPoint)   {
        this.startPoint   =   startPoint;
        this.endPoint   =   endPoint;
    }
}

[解决办法]
应该是这个方法抛了空指针异常
void workArea_mouseDragged(MouseEvent e){
if(startPoint.x!=-1&&startPoint.y!=-1 ){
image = operation.choose();
}
}
楼主DEBUG下e有没有值?如果有,看看函数中其它的变量的值,应该就是这里。

热点排行