Java Applet生命周期

来源:互联网 发布:淘宝店铺号怎么看 编辑:程序博客网 时间:2024/05/19 02:06

生命周期方法

init方法

创建applet之后就会调用init方法,如果Applet的子类有一个初始化过程要完成,那么该子类就应该覆盖init方法。通常这个功能包括从html网页的<applet>标记中获取字符串参数值

start方法

在init方法之后就会调用start方法。当用户浏览过其他网页之后返回到包含这个applet的Web页面时,该方法也会被调用。

每当访问包含applet的web网页时,如果还有任何需要完成的操作,Applet的子类就会覆盖这个方法,例如,一个带动画的applet可以启动一个 定时器来重新开始动画

stop方法

stop方法恰好与start方法相反,start方法是用户返回包含applet的网页时调用的,而stop方法是用户在离开这个网页时调用的。

每当包含applet的Web页面不再可见时,如果还有其他需要执行的操作,Applet的子类就会覆盖这个方法。例如,一个带动画的applet可能会停止定时器来暂停动画。

destory方法

当浏览器正常退出时,就会调用destory方法,以通知applet不再需要它并且应该释放它所占有的资源。stop方法总是在destory方法之前调用。

如果Applet子类在销毁之前还有要完成的操作,这个Applet的子类就会覆盖destory方法。通常情况下是不需要覆盖这个方法的,除非希望释放创建applet所占有的特定资源。

给Applet传递字符串

DisplayMessage.html

<html>    <head>        <title>Passing Strings to Java Applets</title>    </head>    <body>        <p>get a message from the html page and display it</p>        <applet            code = "DisplayMessage.class"            width = 200            height = 50            alt = "you must have a Java 2-enabled browser to view the applet">            <parm name = MESSAGE value = "welcome to Java"/>            <parm name = X value = 20/>            <parm name = Y value = 30/>        </applet>    </body></html>

DisplayMessage.java

import javax.swing.*;public class DisplayMessage extends JApplet{    public void init(){        //从html页面获取参数        String message = getParameter("MESSAGE");        int x = Integer.parseInt(getParameter("X"));        int y = Integer.parseInt(getParameter("Y"));        MessagePanel messagePanel = new MessagePanel(message);        messagePanel.setXCoordinate(x);        messagePanel.setYCoordinate(y);        add(messagePanel);    }}

注:Applet类的getParameter方法只能在创建一个applet的实例之后调用。因此,这个方法不能在applet类的构造方法中调用。应该从init方法中调用它。

MessagePanel .java

import java.awt.*;import javax.swing.*;public class MessagePanel extends JPanel{    private String message = "Hello World";    private int xCoordinate = 20;    private int yCoordinate = 20;    private boolean centered;    private int interval = 10;    public MessagePanel(){}    public MessagePanel(String message){    this.message = message;    }    public String getMessage(){    return message;    }    public void setMessage(String message){        this.message = message;        repaint();    }    public int getXCoordinate(){        return xCoordinate;    }    public void setXCoordinate(int xCoordinate){        this.xCoordinate = xCoordinate;        repaint();    }    public int getYCoordinate(){        return yCoordinate;    }    public void setYCoordinate(int yCoordinate){        this.yCoordinate = yCoordinate;        repaint();    }    public boolean isCentered(){        return centered;    }    public void setCentered(boolean centered){        this.centered = centered;        repaint();    }    public int getInterval(){    return interval;    }    public void setInterval(int interval){        this.interval = interval;        repaint();    }    protected void paintComponent(Graphics g){        super.paintComponent(g);        if(centered == true){            FontMetrics fm = g.getFontMetrics();            int stringWidth = fm.stringWidth(message);            int stringAscent = fm.getAscent();            xCoordinate = getWidth() / 2 - stringWidth / 2;            yCoordinate = getHeight() / 2 + stringAscent / 2;        }        g.drawString(message , xCoordinate , yCoordinate);    }    public void moveLeft(){        xCoordinate -= interval;        repaint();    }    public void moveRight(){        xCoordinate += interval;        repaint();    }    public void moveUp(){        yCoordinate -= interval;        repaint();    }    public void moveDown(){        yCoordinate += interval;        repaint();    }    public Dimension getPreferredSize(){        return new Dimension(200 , 30);    }}
原创粉丝点击