JavaApplets与Android开发入门全解析
立即解锁
发布时间: 2025-08-18 02:24:39 阅读量: 2 订阅数: 13 

### Java Applets与Android开发入门全解析
#### 1. 可拖动Applet的实现
在Java开发中,有时候我们需要实现可拖动的Applet。从Java 6 Update 10开始,就支持可拖动的Applet了,这使得Applet能在自己的进程中运行,从而可以从浏览器拖到桌面。
##### 实现步骤
- **传递参数**:可以通过`deployJava.js`脚本或者`<applet>`标签传递额外参数`draggable`,且其值要设为`true`。以下是使用`<applet>`标签的示例代码:
```html
<html>
<head>
<title>Test page for launching the applet via applet tag</title>
</head>
<body>
<h3>Test page for launching the applet via Applet Tag</h3>
<applet code="org.java7recipes.chapter18.recipe18_15.FirstApplet.class"
archive="/PATH_TO_JAR/FirstApplet.jar"
width=460 height=160>
<param name="draggable" value="true"/>
</applet>
</body>
</html>
```
- **启动Applet**:使用上述解决方案后,访问创建的HTML页面来启动Applet。按住`Alt`键,同时用鼠标左键拖动Applet,就可以将其从浏览器拖到桌面。
##### 注意事项
可拖动的Applet并非在所有浏览器中都能正常工作,所以如果需要这个功能,一定要测试并确保使用兼容的浏览器。
##### 自定义功能
还可以重写`Applet`类中的一些方法,以在Applet从浏览器拖动时执行特定活动,例如:
```java
public boolean isAppletDragStart(MouseEvent e);
public void appletDragStarted();
public void appletDragFinished();
public void setAppletCloseListener(ActionListener l);
public void appletRestored();
```
#### 2. 为Applet加载外部库
在开发Applet时,很多情况下需要使用外部库,比如使用JavaMail API发送邮件时,就需要加载相关的外部库。
##### 实现步骤
- **签名与引用**:必要时对`.jar`文件进行签名,然后在`archive`属性中使用逗号分隔的值列表来包含Applet所需的外部库`.jar`文件。
- **示例代码**:以下是创建简单电子邮件表单的Applet代码:
```java
package org.java7recipes.chapter18.recipe18_17;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailApplet extends Applet implements ActionListener {
TextField from;
TextField to;
TextField smtp;
TextArea message;
Label mailLabel;
Label fromLabel;
Label toLabel;
Label smtpLabel;
Label blank;
Label messageLabel;
Button messageButton;
public void init() {
messageButton = new Button("Send");
mailLabel = new Label("Please fill out the form below to send email.");
fromLabel = new Label("From:");
from = new TextField();
toLabel = new Label("To:");
to = new TextField();
smtpLabel = new Label("SMTP Host:");
smtp = new TextField();
messageLabel = new Label("Message:");
message = new TextArea(null,10,30);
blank = new Label();
this.setLayout(new GridLayout(11,2));
this.add(mailLabel);
this.add(fromLabel);
this.add(from);
this.add(toLabel);
this.add(to);
this.add(smtpLabel);
this.add(smtp);
this.add(messageLabel);
this.add(message);
this.add(messageButton);
messageButton.addActionListener(this);
this.setSize(300, 500);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
sendMail();
this.mailLabel.setText("Message successfully sent");
this.mailLabel.setForeground(Color.GREEN);
repaint();
}
private boolean sendMail() {
boolean result = false;
try {
String from = this.from.getText();
System.out.println(from);
String to = this.to.getText();
String smtp = this.smtp.getText();
String message = this.message.getText();
Properties props = new Properties();
props.put("mail.smtp.host", smtp);
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false);
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] address = new InternetAddress[1];
address[0] = new InternetAddress(to);
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("*** Applet Email ***");
msg.setContent(message, "text/plain");
Transport.send(msg);
result = true;
} catch (javax.mail.MessagingException ex) {
ex.printStackTrace();
result = false;
}
return result;
}
}
```
- **HTML嵌入代码**:以下是用于嵌入Applet的`mailApplet.html`代码:
```html
<html>
<head>
<title>Test page for launching the applet via deployJava.js</title>
</head>
<body>
<h3>Test page for launching the applet via deployJava.js</h3>
<script src="http://java.com/js/deployJava.js"></script>
<script>
var attributes = {
codebase: '/PATH_TO_CODEBASE/',
code: 'org.java7recipes.chapter18.recipe18_17.MailApplet.class',
archive: '/PATH_TO_JAR/MailApplet.jar, /PATH_TO_JAR/mail.jar',
width: 300,
height: 300
};
var parameters = {fontSize:16};
var version = "1.6";
deployJava.runApplet(attributes, parameters, version);
</script>
</body>
</html>
```
##### 原理说明
几乎所有重要的Applet都需要使用外部库,为了让Applet在客户端机器上运行,这些外部库必须包含在Java Applet中。通过修改嵌入Applet的HTML代码,引用所需的外部`.jar`文件,这些文件会和Applet的`.jar`文件一起下载到客户端机器。如果使用`<applet>`标签,也可以在`archive`属性中用逗号分隔的列表添加多个`.jar`文件。
#### 3. 在Applet中使用Swing组件
当应用程序需要使用Swing这个广泛采用的Java组件库来构建所需的GUI时,可以通过扩展`javax.swing.JApplet`类而不是标准的`java.
0
0
复制全文
相关推荐










