JApplet是java.applet.Applet类的子类
JApplet是顶层容器,而JApplet的顶层容器是appletviewer或者为浏览器作为顶层容器都有root pane
而root pane有分为四层,root pane中包含了content pane和glass pane以及其他的兩個面板
glass pane可以用來阻斷鼠標事件
其中有一层是contentPane使用来添加其他组件的
设置布局管理器就是通过contentPane 设置的
默认情况下JApplet的contentPane默认的布局管理器是BorderLayou
而Applet默认的布局管理器
JApplet有四个和生命周期相关的方法init
, start
, stop
, and destroy
由于浏览器不会调用处于生命周期中的方法,所以当Swing组件涉及事件派发线程中
执行相关方法就应该使用SwingUtilities类的
invokeAndWait方法
常用API:
void setContentPane(Container con)
Contianer getContentPane()
void setRootPane(JRootPane)
JRootPane getRootPane()
void setJMenuBar(JMenuBar)
JMenuBar getJMenuBar()
void setGlassPane(Component)
Component getGlassPane()
setLayerredPane(JLayrredPane)
JLayrredPane getLayerredPane()
一个JApplet的例子
JApplet源代码
package myapplet;
import java.awt.Graphics;
import javax.swing.JApplet;
public class HelloWorld extends JApplet{
public void paint(Graphics g){
g.drawString("HelloWorld",5,35);
}
}
MyApplet.html源代码:
<html>
<title>HelloWorld!Applet</title>
<applet code="myapplet.HelloWorld.class" width=200 height=100>
</applet>
</html>
命令行下编译:
javac -d . HelloWorld.java
appletviewer MyApplet.html(或者直接点解MyApplet.html)
需要说明的是用浏览器打开时必须解除加载项阻止
此处HelloWorld.java和MyApplet.html位于同一目录