import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
public class NewsApplet extends Applet {
Image snapshot;
String from, to; public void init() {
from = null == getParameter("from") ? "老鼠" : getParameter("from");
to = null == getParameter("to") ? "大米" : getParameter("to");
snapshot = getImage(getCodeBase(), "snapshot.jpg");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(snapshot, 0);
try {
tracker.waitForID(0);
} catch (Exception e) {
System.out.println("无法下载snapshot.jpg!");
}
}
public void paint(Graphics g) {
g.drawImage(snapshot, 0, 0, snapshot.getWidth(this), snapshot.getHeight(this), this);
g.setFont(new Font("华文彩云", Font.BOLD + Font.ITALIC, 14));
g.setColor(Color.white);
g.drawString(from, 30, 90);
g.drawString("爱", 50, 105);
g.drawString(to, 70, 120);
}
}
OK,保存一下项目,然后可以点击Eclipse的Run菜单的Run as-Java Applet项来预览这个Applet了,是不是很简单?
结合网页
把Applet嵌入网页很简单,只要一个applet标签即可。拿我们的NewsApplet类举例,在Eclipse项目的根目录中新建一个sample.htm,源代码如下:
html
body
applet code="NewsApplet.class" codebase="." width=480
height=384
param name="from" value="Prince Charming"
param name="to" value="Cinderella"
/applet
/body
/html
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"
html
head
title大千世界无奇不有/title
meta http-equiv="Content-Type" content="text/html; charset=utf-8"
/head
body
script language="JavaScript"
// 得到URL中的参数
var urlquery = location.href.split("?");
if (urlquery.length 1) {
// 解析参数并获取from和to
var urlterms = urlquery[1].split("&");
var from = decodeURIComponent(urlterms[0].substring(5));
var to = decodeURIComponent(urlterms[1].substring(3));
// 动态把applet标签写入HTML
document.write(center);
document.write(h1爱琴海上千年罕见的奇观/h1);
document.write(/center);
document.write(hr);
document.write(p align=centerhttp://www.cfan.com.cn 2005年2月14日 记者:GaryCha 展开更多 (50%) 分享 猜你喜欢
下拉加载更多内容 ↓