从案例开始的初学,经常问题表述不清,请多包涵、指点以下是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();
    }
    public void paint(Graphics g) {//draw it
        g.drawImage(offI, 0, 0, width, height, this);
    }
}

解决方案 »

  1.   

    现在还有用Applet的?话说我都没有学过Applet...
      

  2.   


    楼主发表于:2012-07-28 15:06:11
    从案例开始的初学,经常问题表述不清,请多包涵、指点以下是applet图形按钮代码。从书中直接抄下来的。1 需要在什么位置嵌入网页地址么?当前代码运行后神马都没有。2 soundA/B声音文件必须是wav格式么?如果网页本身没有音频文件,可以自己找些mp3代替么?
    可以的3 图片可否是png格式?可以的
    4 图片和音频文件必须存放于代码文件夹么?需要放在固定文件夹的。
    5、对于getDocumentBase() /**
         * Gets the URL of the document in which this applet is embedded. 
         * For example, suppose an applet is contained
         * within the document:
         * <blockquote><pre>
         *    http://java.sun.com/products/jdk/1.2/index.html
         * </pre></blockquote>
         * The document base is:
         * <blockquote><pre>
         *    http://java.sun.com/products/jdk/1.2/index.html
         * </pre></blockquote>
         *
         * @return  the {@link java.net.URL} of the document that contains this
         *          applet.
         * @see     java.applet.Applet#getCodeBase()
         */
        public URL getDocumentBase() {
    return stub.getDocumentBase();
        }这里是放URL的。
    建议楼主当前买的书不要看了。
    现在java方面没有applet了。
    而且图形界面都不用了。
    java主要是插件。
    web方面。
      

  3.   

    个人觉得java se里applet swing这些可以跳过。有兴趣了解下即可