当前位置: 代码迷 >> Java相关 >> 帮忙看看这个java小程序怎么编译和运行
  详细解决方案

帮忙看看这个java小程序怎么编译和运行

热度:147   发布时间:2008-06-29 21:19:35.0
帮忙看看这个java小程序怎么编译和运行
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
//页面响应鼠标事件
public class ImgButton extends Applet implements MouseListener
{
  private int width,height;
  private Image offI,img1,img2,img3;
  private Graphics offG;
  private MediaTracker imageTracker;
  private boolean isMouseEnter = false, isPress = false;
  private Color ButtonColor=Color.yellow,lightC,darkC;
  private URL url;
  private AudioClip soundA, soundB;
  private String param;
  
  public void init(){
  
    param = new String();
   
    param = getParameter("soundA");
    if(param == null)
       param = "midiA.mid";
    soundA = getAudioClip(getDocumentBase(), param);
   
    param = getParameter("soundB");
    if(param == null)
       param = "midiB.mid";
    soundB = getAudioClip(getDocumentBase(), param);
   
    width = getSize().width;
    height = getSize().height;   
   

    param = getParameter("URL");
    try{
       url = new URL(param);
    }catch(MalformedURLException e){}
   
    offI = createImage(width,height);
    offG = offI.getGraphics();
    imageTracker = new MediaTracker(this);
    param = getParameter("Images1");
    img1 = getImage(getCodeBase(), param);
    imageTracker.addImage(img1, 0);

    param = getParameter("Images2");
    img2 = getImage(getCodeBase(), param);
    imageTracker.addImage(img2, 0);

    param = getParameter("Images3");
    img3 = getImage(getCodeBase(), param);
    imageTracker.addImage(img3, 0);
   
    try{
       imageTracker.waitForID(0);
    }catch(InterruptedException e){}


    addMouseListener(this);
  }
  
  public void start(){
   offG.drawImage(img1,0,0,width,height,this);
   repaint();
  }
  
public void mouseClicked(MouseEvent e){
   
}



public void mousePressed(MouseEvent e){
    offG.drawImage(img3,0,0,width,height,this);
    repaint();
    soundA.play();
       System.out.println("soundB play");


}



public void mouseReleased(MouseEvent e){
    offG.drawImage(img2,0,0,width,height,this);
    repaint();
    soundB.play();
    getAppletContext().showDocument(url);
}



public void mouseEntered(MouseEvent e){
    offG.drawImage(img2,0,0,width,height,this);
    repaint();

}



public  void mouseExited(MouseEvent e){
    offG.drawImage(img1,0,0,width,height,this);
    repaint();
}
  
  
  public void paint(Graphics g)
  {
    g.drawImage(offI,0,0,width,height,this);
  }
  


  
}

编译和运行的命令各是什么?没有主程序怎么能运行啊!?
搜索更多相关的解决方案: java  编译  运行  

----------------解决方案--------------------------------------------------------
使用javac ImgButton.java来进行编译,
使用appletviewer来运行,要使用appletviewer来运行程序的话需要在程序后面添加一行:// <applet code=ImgButton width=100 height=200></applet>
这里指定大小和applet的源文件!
----------------解决方案--------------------------------------------------------
applet程序嘛
用jdk命令行的appletviewer可以运行,还有就是要在一个网页中嵌入这个程序的class文件,就是一楼的<applet code=ImgButton width=100 height=200></applet>
就是说要先编译成ImgButton.class,再在一个网页的html的body中加入<applet code=ImgButton width=100 height=200></applet>,打开网页就可以加载applet了
到百度搜下吧,有很具体的描述的。
----------------解决方案--------------------------------------------------------
  相关解决方案