applet AppletJava applet Applet java awt PanelAppletAWT applet
- Slides: 26
applet的层次结构 • Applet类的层次,Java. applet. Applet类实际上是 java. awt. Panel的子类。Applet和AWT类的层次如下图 所示。
applet生命周期的一个示例 import java. awt. *; import java. applet. Applet; public class Ex 10_2 extends Applet{ public void init(){ System. out. println("init()method called!"); } public void start(){ System. out. println("start() method called!"); } public void stop() { System. out. println("stop() method called!"); } public void destroy() { System. out. println("destroy () method called!"); } public void paint(Graphics g){ g. draw. String("Hello World!", 50, 80); System. out. println("paint() method called!"); } }
applet生命周期的一个示例(续) 下面是Ex 10_2. htm文件内容: <HTML> <BODY BGLOLOR = "FFFF 00"> <APPLET CODE = "Ex 10_2. class" WIDTH = "300" HEIGHT = "150"> </APPLET> Ex 10_2的运行结果 </BODY> </HTML>
application与applet的结合示例 import java. awt. *; import java. awt. event. *; import javax. swing. *; public class Ex 10_3 extends JApplet { public void init(){ Container content. Pane = get. Content. Pane(); JLabel label=new JLabel("Application and applet! ", Swing. Constants. CENTER); content. Pane. add(label); } public static void main(String args[]){ final JFrame f = new JFrame(); JApplet applet = new Ex 10_3 (); applet. init(); f. set. Content. Pane(applet. get. Content. Pane()); f. set. Bounds(50, 300, 200); f. set. Title("Application and applet! "); f. set. Visible(true); f. set. Default. Close. Operation(Window. Constants. DISPOSE_ON_CLOSE); f. add. Window. Listener(new Window. Adapter(){ public void window. Closed(Window. Event e){System. exit(0); } }); }}
applet中图像显示的一个示例 例10. 4 在applet中加载一个jpg格式图片。 // 文件名Ex 10_4. java import java. awt. *; import java. awt. eve中nt. *; import java. applet. Applet; public class Ex 10_4 extends Applet{ Image img; //声明Image类型的变量img public void init() { img=get. Image(get. Code. Base(), "flower. jpg"); //加载flower. jpg图片 } public void paint(Graphics g) { g. draw. Image(img, 40, this); //将img画在applet上} } • 下面是相应的Ex 10_4. htm文件内容: <HTML><BODY BGCLOR = "FFFF 00"> <APPLET CODE ="Ex 10_4. class" WIDTH =400 HEIGHT=300> </APPLET> </BODY></HTML>
声音加载和播放的一个示例 例10. 5 一个简单的Audio测试 以下的applet在appletviewer运行时中输出消息“Audio Test”,然后播放audio文件sounds/sun. au: //文件名Ex 10_5. java //假定存在"sounds/sun. au",sounds与Ex 10_5. class在同 一个目录 import java. awt. Graphics; import java. applet; public class Ex 10_5 extends Applet { public void paint(Graphics g) { g. draw. String("Audio Test", 25); play(get. Document. Base(), "sounds/sun. au"); } }
实例1 例10. 9 在applet中鼠标点击的位置处显示“Hello World!”的 字样。 // 文件名Ex 10_9. java import java. awt. *; import java. awt. event. *; import java. applet. Applet; public class Ex 10_9 extends Applet implements Mouse. Listener { int mouse. X=25; int mouse. Y=25; public void init () { add. Mouse. Listener (this); } public void paint(Graphics g) { g. draw. String("Hello World!", mouse. X, mouse. Y); } public void mouse. Pressed(Mouse. Event evt) { mouse. X = evt. get. X(); mouse. Y = evt. get. Y(); repaint(); } public void mouse. Clicked (Mouse. Event e) {} public void mouse. Entered (Mouse. Event e) {} public void mouse. Exited (Mouse. Event e) {} public void mouse. Released (Mouse. Event e) {} }
实例2(续) public void paint(Graphics g) { g. draw. String("Hello World!", mouse. X, mouse. Y); } public void mouse. Pressed(Mouse. Event evt){ mouse. X = evt. get. X(); mouse. Y = evt. get. Y(); repaint(); } public void mouse. Clicked (Mouse. Event e) {} public void mouse. Entered (Mouse. Event e) {} public void mouse. Exited (Mouse. Event e) {} public void mouse. Released (Mouse. Event e) {} } • 程序分析: • 本例中,首先定义鼠标坐标(25, 25),在mouse. Pressed()方法中,先定位 鼠标当前点击的位置坐标,再调用repaint()方法重画图形。然后再 paint()方法中使用g. draw. String("Hello World!", mouse. X, mouse. Y)方法 在鼠标点击处显示Hello World!字符串。方法init()中的通过 add. Mouse. Listener (this)方法注册鼠标监听。
实例2 例10. 10 向applet传递参数的范例。 在一个HTML文件中,上下文为<applet>的<param>标记能够为applet传递配置 信息。例如: <applet code= Ex 10_10. class width=100 height=100> <param name=image value=duke. gif> </applet> 在applet内部,可用方法get. Parameter()来读取这些值。 // 文件名Ex 10_10. java import java. awt. *; import java. applet. *; public class Ex 10_10 extends Applet { Image im; public void init() { URL url = get. Document. Base(); String image. Name = get. Parameter( " image " ); im = get. Image(url, image. Name); } public void paint(Graphics g){ g. draw. Image(im, 0, 0, this); }}
- Import java.awt.event.*
- Import java.applet.*
- Import java.applet.applet
- Applet skeleton in java
- Applet merupakan contoh dari jenis swing
- Tanslate
- 자바 애플릿
- Import java.applet.applet
- Pengertian awt dan swing pada java
- Difference between applet and awt
- Difference between local applet and remote applet
- Import java.awt.*;
- Java awt swing
- Java gui awt
- Setlayout java
- Import javax.swing
- Java
- Import java.awt.*
- Import java.awt.*
- Swing import
- Cracus
- Java color import
- Import java awt event
- Import java.awt.*
- Event
- Abstract
- Text bolo