Java Applet 是早期在网页中嵌入交互式应用程序的技术,java.applet 包提供了创建和运行 Applet 的基础类。
1、Applet:一种可以在网页中运行的小型 Java 程序
2、生命周期:Applet 有明确的生命周期方法
3、沙盒安全:默认在受限环境中运行,保护用户系统
4、已过时:现代浏览器已不再支持 Java Applet
init() // Applet 初始化时调用
start() // Applet 开始运行时调用
stop() // Applet 停止运行时调用
destroy() // Applet 销毁时调用
// 显示方法
paint(Graphics g) // 绘制 Applet 界面import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
// 简单的 Hello World Applet
public class HelloApplet extends Applet {
private String message;
// 初始化方法
public void init() {
// 获取 HTML 参数
String param = getParameter("message");
message = (param != null) ? param : "Hello, Java Applet!";
// 设置背景色
setBackground(Color.WHITE);
}
// 绘制方法
public void paint(Graphics g) {
// 设置字体和颜色
g.setColor(Color.BLUE);
g.setFont(new Font("Arial", Font.BOLD, 20));
// 绘制文本
g.drawString(message, 50, 50);
// 绘制图形
g.setColor(Color.RED);
g.fillRect(50, 70, 200, 100);
g.setColor(Color.GREEN);
g.fillOval(100, 90, 100, 60);
}
}import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class InteractiveApplet extends Applet implements ActionListener {
private Button clickButton;
private int clickCount = 0;
private TextField nameField;
private String userName = "Guest";
public void init() {
setBackground(Color.LIGHT_GRAY);
setLayout(new FlowLayout());
// 创建标签
Label nameLabel = new Label("请输入姓名:");
add(nameLabel);
// 创建文本框
nameField = new TextField(15);
add(nameField);
// 创建按钮
clickButton = new Button("点击我!");
clickButton.addActionListener(this);
add(clickButton);
// 确定按钮
Button confirmButton = new Button("确定");
confirmButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
userName = nameField.getText();
if (userName.trim().isEmpty()) {
userName = "Guest";
}
repaint(); // 重绘界面
}
});
add(confirmButton);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == clickButton) {
clickCount++;
repaint();
}
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLUE);
g.setFont(new Font("Serif", Font.BOLD, 16));
g.drawString("欢迎, " + userName + "!", 20, 120);
g.setColor(Color.RED);
g.drawString("按钮点击次数: " + clickCount, 20, 150);
// 根据点击次数绘制不同图形
for (int i = 0; i < clickCount % 10; i++) {
g.setColor(new Color(
(int)(Math.random() * 255),
(int)(Math.random() * 255),
(int)(Math.random() * 255)
));
g.fillRect(20 + i * 25, 170, 20, 20);
}
}
}import java.applet.Applet;
import java.awt.*;
public class AnimationApplet extends Applet implements Runnable {
private Thread animationThread;
private int xPosition = 0;
private int direction = 5; // 移动方向
private boolean running = false;
public void init() {
setBackground(Color.BLACK);
setSize(400, 200);
}
public void start() {
if (animationThread == null) {
animationThread = new Thread(this);
running = true;
animationThread.start();
}
}
public void stop() {
running = false;
animationThread = null;
}
public void run() {
while (running) {
// 更新位置
xPosition += direction;
// 边界检测
if (xPosition <= 0 || xPosition >= getWidth() - 50) {
direction = -direction; // 反转方向
}
repaint(); // 请求重绘
try {
Thread.sleep(30); // 控制动画速度
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g) {
// 清除背景
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
// 绘制移动的球
g.setColor(Color.YELLOW);
g.fillOval(xPosition, 75, 50, 50);
// 绘制轨迹
g.setColor(Color.GRAY);
for (int i = 0; i < getWidth(); i += 10) {
g.drawLine(i, 150, i, 155);
}
// 显示信息
g.setColor(Color.WHITE);
g.drawString("动画演示 - 球体弹跳", 10, 20);
}
}<!DOCTYPE html>
<html>
<head>
<title>Java Applet 示例</title>
</head>
<body>
<h1>Java Applet 演示</h1>
<!-- 基础 Applet -->
<applet code="HelloApplet.class" width="300" height="200">
<param name="message" value="欢迎使用 Applet!">
您的浏览器不支持 Java Applet。
</applet>
<!-- 交互式 Applet -->
<applet code="InteractiveApplet.class" width="400" height="250">
您的浏览器不支持 Java Applet。
</applet>
<!-- 动画 Applet -->
<applet code="AnimationApplet.class" width="400" height="200">
您的浏览器不支持 Java Applet。
</applet>
</body>
</html>1、编译 Java 文件。
javac *.java2、创建 HTML 文件:使用上面的 HTML 代码。
3、运行 Applet。
使用 appletviewer 工具:
appletviewer example.html或在支持 Java 的浏览器中打开 HTML 文件
1、安全性限制:Applet 在沙盒环境中运行,对系统资源的访问受限
2、现代替代方案:由于安全问题和浏览器支持的变化,建议使用以下替代技术:
3、已过时:Java 9 开始标记 Applet API 为过时,Java 11 中已移除
虽然 Java Applet 技术已过时,但了解其原理对于理解 Java 图形编程和历史发展仍有价值。