Applet与事件

写在之前

以下是《Java8编程入门官方教程》中的一些知识,如有错误,烦请指正。涉及的程序如需下载请移步http://down1.tupwk.com.cn/qhwkdownpage/978-7-302-38738-1.zip

applet的基础知识

applets是设计用于在Internet间传输,在浏览器上运行的小程序。有两种基本类型:基于抽象窗口工具包的applet和基于Swing的applet。两者都支持创建用户图形界面。

import java.awt.*; 
import java.applet.*; 
/* 
 
 
*/ 
 
public class SimpleApplet extends Applet { 
  public void paint(Graphics g) { 
    g.drawString("Java makes applets easy.", 20, 20); 
  } 
}
  1. 运行javac SimpleApplet.java进行编译
  2. 运行appletviewer SimpleApplet.java命令,大家注意后面的参数是.java文件而不是类名。

applet架构

applet是基于GUI的程序。applet由事件驱动。由用户发起与applet的交互。

applet框架

// An AWT-based Applet skeleton. 
import java.awt.*; 
import java.applet.*; 
/* 
 
 
*/ 
 
public class AppletSkel extends Applet { 
  // Called first. 
  public void init() { 
    // initialization 
  } 
 
  /* Called second, after init().  Also called whenever 
     the applet is restarted. */ 
  public void start() { 
    // start or resume execution 
  } 
 
  // Called when the applet is stopped. 
  public void stop() { 
    // suspends execution 
  } 
 
  /* Called when applet is terminated.  This is the last 
     method executed. */ 
  public void destroy() { 
    // perform shutdown activities 
  } 
 
  // Called when an AWT-based applet's window must be restored. 
  public void paint(Graphics g) { 
    // redisplay contents of window 
  } 
}

虽然框架没有执行实际的操作,但是仍然可以编译执行。

applet的初始化和终止

开始运行:init--start--paint

终止:stop--destroy\

请求重绘

通过repaint()和update方法实现。

/*  
   Try This 14-1 
 
   A simple banner applet. 
 
   This applet creates a thread that scrolls 
   the message contained in msg right to left 
   across the applet's window. 
*/ 
import java.awt.*; 
import java.applet.*; 
/* 
 
 
*/ 
 
public class Banner extends Applet implements Runnable { 
  String msg = " Java Rules the Web "; 
  Thread t; 
  boolean stopFlag; 
 
  // Initialize t to null.  
  public void init() { 
    t = null; 
  } 
 
  // Start thread 
  public void start() { 
    t = new Thread(this); 
    stopFlag = false; 
    t.start(); 
  } 
 
  // Entry point for the thread that runs the banner. 
  public void run() { 
 
    // Display banner  
    for( ; ; ) { 
      try { 
        repaint(); 
        Thread.sleep(250); 
        if(stopFlag)
          break; 
      } catch(InterruptedException exc) {} 
    } 
  } 
 
  // Pause the banner. 
  public void stop() { 
    stopFlag = true; 
    t = null; 
  } 
 
  // Display the banner. 
  public void paint(Graphics g) { 
    char ch; 

    ch = msg.charAt(0); 
    msg = msg.substring(1, msg.length()); 
    msg += ch; 
    g.drawString(msg, 50, 30); 
  } 
}

使用状态窗口

void showStatus(String msg)
// Using the Status Window. 
import java.awt.*; 
import java.applet.*; 
/* 
 
 
*/ 
 
public class StatusWindow extends Applet{ 
  // Display msg in applet window. 
  public void paint(Graphics g) { 
    g.drawString("This is in the applet window.", 10, 20); 
    showStatus("This is shown in the status window."); 
  } 
}

向applet传递形参

String getParameter(String paramName)
// Pass a parameter to an applet. 
import java.awt.*; 
import java.applet.*; 
 
//html形参
/* 
 
 
 
 
 
*/ 
 
public class Param extends Applet { 
  String author; 
  String purpose; 
  int ver; 
 
  public void start() { 
    String temp; 
 
    author = getParameter("author"); 
    if(author == null) author = "not found"; 
 
    purpose = getParameter("purpose"); 
    if(purpose == null) purpose = "not found"; //检查形参是否存在
 
    temp = getParameter("version"); 
    try { 
      if(temp != null) 
        ver = Integer.parseInt(temp); 
      else 
        ver = 0; 
    } catch(NumberFormatException exc) { //确保数值转换成功
        ver = -1; // error code 
    } 
  } 
 
  public void paint(Graphics g) { 
    g.drawString("Purpose: " + purpose, 10, 20); 
    g.drawString("By: " + author, 10, 40); 
    g.drawString("Version: " + ver, 10, 60); 
  } 
}

Applet类

所有的基于AWT的applet都是Applet类的子类。

委派事件模型

概念:一个源产生一个事件,然后将它送到一个或多个监听器那里。

事件

委派模型中,描述源中状态变化的对象。

事件源:产生事件的对象。一个源必须注册监听器,以求可以接受关于特定时间类型的通知。

public void addTypeListener(TypeListener el)

源必须提供一个方法,以允许侦听器取消对某种特定类型事件的注册。

public void removeTypeListener(TypeListener el)

事件侦听器:这是在事件发生时被告知的那个对象。需要两个条件:必须向一个或多个源注册;必须实现一些方法,接受并处理这些通知。

使用模型:

  1. 在侦听器中国实现相应接口,使其接收所需类型的事件
  2. 实现代码将侦听器作为事件通知的接收者来注册或者取消注册
// Demonstrate the mouse event handlers. 
import java.awt.*; 
import java.awt.event.*; 
import java.applet.*; 
/* 
   
   
*/ 
 
public class MouseEvents extends Applet 
  implements MouseListener, MouseMotionListener { 
 
  String msg = ""; 
  int mouseX = 0, mouseY = 0; // coordinates of mouse 
 
  public void init() { 
     addMouseListener(this); //注册该类作为鼠标事件的监听者
     addMouseMotionListener(this); 
  } 
 
  // Handle mouse clicked. 
  public void mouseClicked(MouseEvent me) { 
    mouseX = 0; 
    mouseY = 10; 
    msg = "Mouse clicked."; 
    repaint(); 
  } 
 
  // Handle mouse entered. 
  public void mouseEntered(MouseEvent me) { 
    mouseX = 0; 
    mouseY = 10; 
    msg = "Mouse entered."; 
    repaint(); 
  } 
 
  // Handle mouse exited. 
  public void mouseExited(MouseEvent me) { 
    mouseX = 0; 
    mouseY = 10; 
    msg = "Mouse exited."; 
    repaint(); 
  } 
 
  // Handle button pressed. 
  public void mousePressed(MouseEvent me) { 
    // save coordinates 
    mouseX = me.getX(); 
    mouseY = me.getY(); 
    msg = "Down"; 
    repaint(); 
  } 
 
  // Handle button released. 
  public void mouseReleased(MouseEvent me) { 
    // save coordinates 
    mouseX = me.getX(); 
    mouseY = me.getY(); 
    msg = "Up"; 
    repaint(); 
  } 
 
  // Handle mouse dragged. 
  public void mouseDragged(MouseEvent me) { 
    // save coordinates 
    mouseX = me.getX(); 
    mouseY = me.getY(); 
    msg = "*"; 
    showStatus("Dragging mouse at " + mouseX + ", " + mouseY); 
    repaint(); 
  } 
 
  // Handle mouse moved. 
  public void mouseMoved(MouseEvent me) { 
    // show status 
    showStatus("Moving mouse at " + me.getX() + ", " + me.getY()); 
  } 
 
  // Display msg in applet window at current X,Y location. 
  public void paint(Graphics g) { 
    g.drawString(msg, mouseX, mouseY); 
  } 
}

其他Java关键字

transient:在存储对象时,它的值不需要存储。

volatile:告知编译器,程序的其他部分可以任意改动该变量。

instanceof:如果是指定的类型,或者可以强制转换成指定类型,运算符返回ture

assert:程序开发期间创建断言。

assert condition;
assert n>0;//如果断言失败抛出AssertionError
  
 assert condition: expr;//如果断言失败,该值被替换成它的字符串格式,并在断言失败时显示、

native:申明一个本机方法,同时必须编写好,并按照步骤来链接Java代码。

你可能感兴趣的:(Applet与事件)