In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
这篇文章主要讲解了"Java2怎么控制APPLET的运行",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java2怎么控制APPLET的运行"吧!
1.APPLET:运行在支持Java的web浏览器内的JAVA小程序。
浏览器将对Web页进行初始化和启动applet程序;当我们不需要显示该Web页时,
浏览器将启动相应的方法终止applet程序的运行。
1.1由下面四个方法来控制APPLET的运行:
(1)init() 打开带有applet的文档时,调用init()方法初始化applet
(2)start() 打开带有applet的文档时,在init()方法之后调用start()方法,启动applet
(3)stop() 关闭带有applet的文档时调用。注意:stop()总在destroy()方法之前被调用
(4)destroy() 关闭浏览器时调用。调用destroy()是为了整理曾经使用的资源
一个简单的applet例子://StarterApplet.java
import java.applet.Applet;
import java.awt.Label;
public class StarterApplet extends Applet {
private Label label;
public void init() {
System.out.println("Applet::init()");
}
public void start() {
System.out.println("Applet::start()");
label = new Label("Starter");
add(label);
}
public void stop() {
System.out.println("Applet::stop()");
remove(label);
}
public void destroy() {
System.out.println("Applet::destroy()");
}
}
file://随便一个HTML文件如001.html,加入
file://命令行下输入:appletviewer 001.html,看到了吧,当然也可以用浏览器看。再强调一遍stop()总在destroy()方法之前被调用!
1.2要更新java.awt.Component的显示方式用下面三个方法:
paint() 绘制构件
repaint() 尽可能早地调度构件的update方法调用
update() 重画构件,默认方案为刷新屏幕并调用paint方法
2.Java应用程序:在Java解释器中运行
与APPLET的区别: (1)Java应用程序必须有main()方法
(2)Java应用程序需要有一个窗口,则它必须扩展AWT的Frame类
(3)Java应用程序没有与applet相同的安全性约束
一个简单的应用程序例子://StarterApplication.java
import java.awt.Event;
import java.awt.Frame;
import java.awt.event.*;
import java.awt.Label;
public class StarterApplication extends Frame {
public static void main(String args[]) {
StarterApplication app =new StarterApplication("Starter Application");
app.setSize(300,100);
app.show ();
System.out.println("StarterApplication::main()");
}
public StarterApplication(String frameTitle) {
super(frameTitle);
add (new Label("Starter", Label.CENTER), "Center");//默认的布局管理器为BorderLayout
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
dispose();
System.exit(0);
}
});
}
}
3.既是APPLET又是应用程序:既有main方法又extends Applet。//StarterCombined.java
import java.applet.Applet;
import java.awt.Event;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.*;
public class StarterCombined extends Applet {
private Label label;
public static void main(String args[]) {
StarterCombinedFrame app =new StarterCombinedFrame("Starter Application");
app.setSize(300,100);
app.show ();
System.out.println("StarterCombinedFrame::main()");
}
public void init() {
System.out.println("Applet::init()");
}
public void start() {
System.out.println("Applet::start()");
label = new Label("Starter");
add(label);
}
public void stop() {
System.out.println("Applet::stop()");
remove(label);
}
public void destroy() {
System.out.println("Applet::destroy()");
}
}
class StarterCombinedFrame extends Frame {
public StarterCombinedFrame(String frameTitle) {
super(frameTitle);
StarterCombined applet = new StarterCombined();
applet.start();
add (applet, "Center");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
dispose();
System.exit(0);
}
});
}
}
由DOS中打印的文字看到:如果程序作为一个applet程序运行,那么main()方法将被忽略。
感谢各位的阅读,以上就是"Java2怎么控制APPLET的运行"的内容了,经过本文的学习后,相信大家对Java2怎么控制APPLET的运行这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.