小白教程

 找回密码
 立即注册
小白教程 首页 系列教程 Java系列教程 查看内容

Java Applet基础

发布者: 小白教程



事件处理

Applet类从Container类继承了许多事件处理方法。Container类定义了几个方法,例如:processKeyEvent()和processMouseEvent(),用来处理特别类型的事件,还有一个捕获所有事件的方法叫做processEvent。

为了响应一个事件,applet必须重写合适的事件处理方法。

  1. import java.awt.event.MouseListener;
  2. import java.awt.event.MouseEvent;
  3. import java.applet.Applet;
  4. import java.awt.Graphics;
  5. public class ExampleEventHandling extends Applet
  6. implements MouseListener {
  7. StringBuffer strBuffer;
  8. public void init() {
  9. addMouseListener(this);
  10. strBuffer = new StringBuffer();
  11. addItem("initializing the apple ");
  12. }
  13. public void start() {
  14. addItem("starting the applet ");
  15. }
  16. public void stop() {
  17. addItem("stopping the applet ");
  18. }
  19. public void destroy() {
  20. addItem("unloading the applet");
  21. }
  22. void addItem(String word) {
  23. System.out.println(word);
  24. strBuffer.append(word);
  25. repaint();
  26. }
  27. public void paint(Graphics g) {
  28. //Draw a Rectangle around the applet's display area.
  29. g.drawRect(0, 0,
  30. getWidth() - 1,
  31. getHeight() - 1);
  32. //display the string inside the rectangle.
  33. g.drawString(strBuffer.toString(), 10, 20);
  34. }
  35. public void mouseEntered(MouseEvent event) {
  36. }
  37. public void mouseExited(MouseEvent event) {
  38. }
  39. public void mousePressed(MouseEvent event) {
  40. }
  41. public void mouseReleased(MouseEvent event) {
  42. }
  43. public void mouseClicked(MouseEvent event) {
  44. addItem("mouse clicked! ");
  45. }
  46. }

如下调用该applet:

  1. <html>
  2. <title>Event Handling</title>
  3. <hr>
  4. <applet code="ExampleEventHandling.class" width="300" height="300">
  5. </applet>
  6. <hr>
  7. </html>

最开始运行,applet显示 "initializing the applet. Starting the applet.",然后你一点击矩形框,就会显示"mouse clicked" 。


显示图片

applet能显示GIF,JPEG,BMP等其他格式的图片。为了在applet中显示图片,你需要使用java.awt.Graphics类的drawImage()方法。

如下实例演示了显示图片的所有步骤:

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.net.*;
  4. public class ImageDemo extends Applet
  5. {
  6. private Image image;
  7. private AppletContext context;
  8. public void init()
  9. {
  10. context = this.getAppletContext();
  11. String imageURL = this.getParameter("image");
  12. if(imageURL == null)
  13. {
  14. imageURL = "java.jpg";
  15. }
  16. try
  17. {
  18. URL url = new URL(this.getDocumentBase(), imageURL);
  19. image = context.getImage(url);
  20. }catch(MalformedURLException e)
  21. {
  22. e.printStackTrace();
  23. // Display in browser status bar
  24. context.showStatus("Could not load image!");
  25. }
  26. }
  27. public void paint(Graphics g)
  28. {
  29. context.showStatus("Displaying image");
  30. g.drawImage(image, 0, 0, 200, 84, null);
  31. g.drawString("www.javalicense.com", 35, 100);
  32. }
  33. }

如下调用该applet:

  1. <html>
  2. <title>The ImageDemo applet</title>
  3. <hr>
  4. <applet code="ImageDemo.class" width="300" height="200">
  5. <param name="image" value="java.jpg">
  6. </applet>
  7. <hr>
  8. </html>

上一篇:Java 多线程编程下一篇:Java 文档注释

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-20 06:22 , Processed in 0.020439 second(s), 18 queries .