Java Applet 程序运行后 运行结果出现提示【启动:未初始化小程序 】这是怎么回事?怎样调啊/
程序没有错误!比如运行一下程序时,出现我所说的运行结果提示://P248页例7-16的代码importjava.applet.*;importjava.awt.*;imp...
程序没有错误!比如运行一下程序时,出现我所说的运行结果提示:
//P248页例7-16的代码
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class TestCanvas extends Applet
{
CanvasDraw cd;
public void init()
{
cd=new CanvasDraw(new Dimension(200,180),this);
cd.setBackground(Color.pink);
add(cd);
cd.requestFocus();
}
}
class CanvasDraw extends Canvas
{
Applet m_parat;
boolean md_Flag=false;
int startX=0,startY=0,currentX=0,currentY=0;
StringBuffer sb= new StringBuffer();
CanvasDraw(Dimension d,Applet p)
{
m_parat=p;
setSize(d);
setBackground(Color.gray);
addKeyListener(new KeyEventProcess());
addMouseListener(new MouseAdpt());
addMouseMotionListener(new MouseMotionAdpt());
}
void setStart(int x, int y)
{
startX=x;
startY=y;
}
void setCurrent(int x,int y)
{
currentX=x;
currentY=y;
}
void setMouseDragged(boolean b)
{
md_Flag=b;
}
void showMeg(String s)
{
m_parat.showStatus(s);
}
void clearAll()
{
startX=0;
startY=0;
currentX=0;
currentY=0;
repaint();
}
public void paint(Graphics g)
{
g.drawString( "("+currentX+","+currentY+")",10,20);
if(md_Flag)
g.drawLine(startX, startY, currentX, currentY);
}
class KeyEventProcess implements KeyListener //内部类实现接口
{
public void keyTyped(KeyEvent e)
{
char ch=e.getKeyChar();
if(ch=='c'){
sb.setLength(0);
clearAll();
}
else
sb.append(ch);
showMeg("点击"+sb.toString());
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
class MouseAdpt extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
setStart(e.getX(),e.getY());
showMeg("你开始画线");
}
public void mouseReleased(MouseEvent e)
{
if((startX!=currentX)|(startY!=currentY))
showMeg("你画出了一条直线");
}
public void mouseEntered(MouseEvent e)
{
showMeg("鼠标进入画布");
}
public void mouseExited(MouseEvent e)
{
showMeg("鼠标移除画布");
}
}
class MouseMotionAdpt extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent e)
{
setCurrent(e.getX(),e.getY());
setMouseDragged(false);
repaint(10,0,60,30);
}
public void mouseGragged(MouseEvent e)
{
setCurrent(e.getX(),e.getY());
setMouseDragged(true);
repaint();
}
}//end of MouseMotionAdpt(内部类)
}//end of CanvasDraw 展开
//P248页例7-16的代码
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class TestCanvas extends Applet
{
CanvasDraw cd;
public void init()
{
cd=new CanvasDraw(new Dimension(200,180),this);
cd.setBackground(Color.pink);
add(cd);
cd.requestFocus();
}
}
class CanvasDraw extends Canvas
{
Applet m_parat;
boolean md_Flag=false;
int startX=0,startY=0,currentX=0,currentY=0;
StringBuffer sb= new StringBuffer();
CanvasDraw(Dimension d,Applet p)
{
m_parat=p;
setSize(d);
setBackground(Color.gray);
addKeyListener(new KeyEventProcess());
addMouseListener(new MouseAdpt());
addMouseMotionListener(new MouseMotionAdpt());
}
void setStart(int x, int y)
{
startX=x;
startY=y;
}
void setCurrent(int x,int y)
{
currentX=x;
currentY=y;
}
void setMouseDragged(boolean b)
{
md_Flag=b;
}
void showMeg(String s)
{
m_parat.showStatus(s);
}
void clearAll()
{
startX=0;
startY=0;
currentX=0;
currentY=0;
repaint();
}
public void paint(Graphics g)
{
g.drawString( "("+currentX+","+currentY+")",10,20);
if(md_Flag)
g.drawLine(startX, startY, currentX, currentY);
}
class KeyEventProcess implements KeyListener //内部类实现接口
{
public void keyTyped(KeyEvent e)
{
char ch=e.getKeyChar();
if(ch=='c'){
sb.setLength(0);
clearAll();
}
else
sb.append(ch);
showMeg("点击"+sb.toString());
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
class MouseAdpt extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
setStart(e.getX(),e.getY());
showMeg("你开始画线");
}
public void mouseReleased(MouseEvent e)
{
if((startX!=currentX)|(startY!=currentY))
showMeg("你画出了一条直线");
}
public void mouseEntered(MouseEvent e)
{
showMeg("鼠标进入画布");
}
public void mouseExited(MouseEvent e)
{
showMeg("鼠标移除画布");
}
}
class MouseMotionAdpt extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent e)
{
setCurrent(e.getX(),e.getY());
setMouseDragged(false);
repaint(10,0,60,30);
}
public void mouseGragged(MouseEvent e)
{
setCurrent(e.getX(),e.getY());
setMouseDragged(true);
repaint();
}
}//end of MouseMotionAdpt(内部类)
}//end of CanvasDraw 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询