日期:2014-05-20  浏览次数:20667 次

初学,问题与提问方式似乎都很幼稚……
从案例开始的初学,经常问题表述不清,请多包涵、指点

以下是applet图形按钮代码。从书中直接抄下来的。

1 需要在什么位置嵌入网页地址么?当前代码运行后神马都没有。

2 soundA/B声音文件必须是wav格式么?如果网页本身没有音频文件,可以自己找些mp3代替么?

3 图片可否是png格式?

4 图片和音频文件必须存放于代码文件夹么?

5 getDocumentBase()能否给解释一下?

手册还在熟悉阶段,有点晕,所以想用这种偷懒方式打算稍微积累下先。手册的使用方法是一直在学的,不过……请多指教!

因为问题会很多,每次只能给20分,请见谅!

package JavaApplet;
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();
  //get sound file from html
  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);
  //get size of fig from html
  width = getSize().width;
  height = getSize().height;
  //get url from html
// param = getParameter ("URL");
  param = getParameter ("C:\\Users\\leo\\Desktop\\2.png");
  try {
  url = new URL(param);
  }catch (MalformedURLException e){
   
  }
  //set fig and load to fig-tracker
  offI = createImage(width, height);
  offG = offI.getGraphics();
  imageTracker = new MediaTracker(this);
  param = getParameter("Images1");
  img1 = getImage(getCodeBase(), param);
  param = getParameter("Images2");
  img2 = getImage(getCodeBase(), param);
  param = getParameter("Images3");
  img3 = getImage(getCodeBase(), param);
  try{
  imageTracker.waitForID(0);
  }catch (InterruptedException e){
   
  }
  //set mouse-listener
  addMouseListener(this);
  }
  public void start(){//method of applet start
  offG.drawImage(img1, 0, 0, width, height, this);
  repaint();
  }
  public void mouseClicked(MouseEvent e){//mouse-click event
   
  }
  public void mousePressed(MouseEvent e){//press the mouse not release
  offG.drawImage(img3, 0, 0, width, height, this);
  repaint();
  soundA.play();
  }
  public void mouseReleased(MouseEvent e){//release the mouse
  offG.drawImage(img2, 0, 0, width, height, this);
  repaint();
  soundB.play();
  getAppletContext().showDocument(url);
  }
  public void mouseEntered(MouseEvent e){//mouse enter
  offG.drawImage(img2, 0, 0, width, height, this);
  repaint();
  }
  public void mouseExited(MouseEvent e){//mouse exit
  offG.drawImage(img1, 0, 0, width, height, this);
  repaint();