Java应用技巧

类别:Java 点击:0 评论:0 推荐:


---- 1. Applet 与 Application 合 并 

---- Java 是 一 种 建 立 分 布 应 用 程 序 的 技 术 。 它 是 网 络 编 
程 的 最 优 秀 工 具 。 使 Internet 的 应 用 从 通 讯 工 具 扩 展 到 能
 够 运 行 成 熟 应 用 程 序 的 网 络 。 

---- Java 程 序 有 两 种 类 型:Applet 和 Application 。 简 单 的 讲 ,
Applet 就 是 嵌 入Web 文 档 的 程 序 , 而Application 则 是 所 有 其 它
 类 型 的 程 序 。 

---- 就Java 而 言 ,Applet 与 Application 的 大 小 和 复 杂 性 都 没 有
 限 制 , 但 是 因 为Applet 主 要 是 用 于 网 络 通 讯 , 由 于 通 讯 
速 度 有 限 , 下 载 时 间 较 长 , 因 而 Applet 一 般 来 说 规 模 较 
小 , 而 对 于Application 则 无 此 顾 虑 。 

---- Applet 与 Application 之 间 的 技 术 差 别 来 源 于 其 运 行 环 境
 的 差 别 。 Applet 需 要 来 自 浏 览 器 的 大 量 信 息: 浏 览 器 客 
户 机 的 位 置 和 大 小 、 嵌 入 主HTML 文 档 的 参 数 、 初 始 化 过 
程(init) 、 启 动 过 程(start) 、 停 止 过 程(stop) 、 终 止 过 程(de
story) 、 绘 图 过 程(paint) 等 等 , 而Application 则 相 对 要 简 单 
得 多 , 它 来 自 外 部 世 界 的 唯 一 输 入 就 是 命 令 行 参 数 。 


---- 对 于 Applet 必 须 做 为java.applet.Applet 的 子 类 , 而Applicat
ion 则 必 须 有 一 个 公 共 的 方 法 main( ) 。 其 次 , 两 者 的 主 线
 程 是 不 同 的 ,Applet 是 由 方 法 init( ) 来 对applet 进 行 初 始 化
 的 , 而Application 则 由 方 法main( ) 来 开 始 运 行 程 序 的 。 一 
般 地 ,Java 的 Applet 和 Application 是 完 全 遵 照 以 上 的 原 则 进
 行 编 程 的 , 但 是 , 我 们 可 以 运 用 技 巧 写 出 即 是Applet 又 
是 Application 的 程 序 来 。 这 样 , 即 可 以 使 我 们 更 进 一 步 了
 解Java 的 内 部 结 构 又 可 以 使 同 一 程 序 运 行 于 不 同 的 运 行
 环 境 、 还 可 以 提 高 研 究Java 的 兴 趣 。 

---- 例 程 如 下: 


      import java.applet.*;

      import java.awt.*;

      import java.awt.event.*;

       



      public class Example extends Applet implements 

                       ActionListener{ // #1

         public static void main(String[] args){

           Frame win=new Frame("Example");

           Example example=new Example();

           win.add(揅enter", example);

           example.init();             // #2

           win.setSize(600,360);

           win.setVisible(true);

         }

      public void init(){  // #3

           Button button;

           Label  label;

           button=new Button(" OK "):

           button.setBounds(280,200,100,20);

           button.addActionListener(this);

           label=new Label();

           label.setBounds(260,100,200,20);

           add(button);

           add(label);

         }

       public void actionPerformed(ActionEvent e){

           Object source=e.getSource();

           if(source==button){

             label.setText(" It is a example !");

           }

         }

      }

---- 在 程 序 中 的 #1 处 , 必 须 让 程 序 继 承java.applet.Applet 类
 这 是 做 为Applet 的 必 要 条 件 , 在 #2 处 , 需 要 生 成 本 类 的
 一 个 实 例 example , 通 过 此 实 例 来 调 用init() 方 法 , #3 是 
init() 方 法 。 
---- 如 果 把 该 程 序 看 作Applet , 它 只 是 覆 盖 了init() 方 法 和a
ctionPerformed( ) 方 法 来 响 应Button 事 件 。 如 果 作 为Application
 , 则 由main( ) 方 法 开 始 , 先 生 成 程 序 本 身 的 实 例 将 程 序
 加 入 窗 口 , 然 后 调 用init() 方 法 。 编 译 此 程 序 , 即 可 以 
在 提 示 下 作 为Application 直 接 运 行 , 又 可 以 作 为Applet 送 入
 任 何 浏 览 器 。 

---- 2. Java 中 的 发 声 提 示 

---- Java 作 为 一 种 网 络 编 程 语 言 , 在 浏 览 器 中 即 可 以 进 
行 动 画 演 示 , 也 可 以 播 放 声 音 。 在 人 机 的 交 互 过 程 中 ,
 为 了 加 强 效 果 或 起 到 提 示 的 作 用 , 需 要 加 入 声 音 。 

---- Java 的 包 java.applet 中 有AudioClip 接 口 , 此 接 口 封 装 有 
关 声 音 片 断 的 一 些 常 用 方 法 。 用 法 如 下: 


       AudioClip audio;

         audio=getAudioClip(getCodeBase(),"Hi.au");

         audio.play();

---- 第 一 行 生 成 接 口AudioClip 的 一 个 变 量 , 第 二 行 利 用 此
 变 量 取 得 声 音 文 件 Hi.au , 此 文 件 与 程 序 本 身 在 同 一 目 录
 下,getCodeBase() 方 法 是 用 来 取 得Applet 的class 文 件 的URL 地 址
 。 第 三 行 是 播 放 声 音 文 件 。 在Applet 中 利 用 此 用 法 可 在 
浏 览 器 中 发 出 声 音 。 那 么 , 在Application 中 是 否 也 可 以 用
 此 方 法 来 发 出 声 音 呢 ? 不 可 以 。 因 为 接 口AudioClip 是 在 
包 java.applet 中 , 而 此 包 只 适 用 于Applet 。 是 否 可 以 用 其 它
 方 法 来 实 现 呢 ? 我 们 可 以 利 用 1 中 的 技 巧 来 编 写 一 个 即
 是Applet 又 是 Application 的 程 序 试 试 。 结 果 , 还 是 不 行 。 
这 是 因 为play() 方 法 只 能 在Applet 中 实 现 , 对 于Applicationpla
y() 方 法 是 不 能 够 被 调 用 的 。 
---- 那 么 , 如 何 在Application 中 实 现 发 声 提 示 呢 ? 

---- 记 得 VB 中 有 beep 语 句 来 使 系 统 发 声 器(System Speaker ) 发
 声 , 那 么 ,Java 中 也 应 有 类 似 的 方 法 。 

---- 在 Java 的 java.awt.Toolkit 类 中 有 方 法beep() 是 来 实 现 这 一
 功 能 的 。 类 Toolkit 是 抽 象 类 , 它 是 实 现AWT 的 所 有 工 具 的
 父 类 。 Java 中 的 抽 象 类 是 不 能 够 实 例 化 的 , 但 是 一 般 地
 , 抽 象 类 可 以 生 成 变 量 , 然 后 利 用 抽 象 类 中 的 某 一 方 
法 来 取 得 此 类 的 替 代 品 。 在 Toolkit 中 是 利 用 getDefaultTool
kit() 方 法 来 实 现 的 。 现 在 给 出 一 个 实 例: 这 是 一 个 客 户
 机/ 服 务 器 的Application 。 当 服 务 器 运 行 时 如 果 有 客 户 机 
与 服 务 器 相 连 , 则 服 务 器 会 自 动 发 声 警 报 提 示 服 务 器 端
 的 用 户 有 客 户 要 与 自 己 进 行 对 话 。 

---- 程 序 如 下: 


 服 务 器:

import java.util.*;

import java.io.*;

import java.net.*;

import java.awt.*;



public class ServerT{

  public static void main(String[] args){

    Server server;

    String clientRequest;

    boolean quit=false;

    server=new Server(8001);

    while(!quit){

      DataInputStream keyboard=new DataInputStream(System.in);

      try{

        clientRequest=server.in.readLine();

        if(clientRequest.trim().equals("CLOSE")){

          System.out.println("Client says: "+clientRequest);

          System.exit(1);

        }

        System.out.println("Client says: "+clientRequest);

        server.out.println(keyboard.readLine());

       }catch(IOException e){

        System.out.println("IOException in server.in.readLine()"+e);

        System.exit(1);

      }

    }

  }

}

class Server{

  private ServerSocket server;

  private Socket socket;

  public DataInputStream in;

  public PrintStream out;

  public Server(int port){

    try{

      server=new ServerSocket(port);

      System.out.println("\n

*************************************************");

      System.out.println("\n @(#)Net Applecation 

              Version 1.00 97/12/30 ");

      System.out.println(" Copyright (c) 1997

              (Lui DongBing) All Rights Reserved.");

      System.out.println("\n 

***************************************************");

      System.out.println("\n Server is: \n "+server);

      socket=server.accept();

      for(int i=0;I < 260;i++){ // 发 声 提 示

Toolkit.getDefaultToolkit().beep();

}

System.out.println("\n Server is ready ! \n");

in=new DataInputStream(socket.getInputStream());

out=new PrintStream(socket.getOutputStream());

out.println("We connect in "+new Date());

}catch(IOException e){

System.out.println("Server is failied !");

}

}

}





客 户 机 :

import java.util.*;

import java.io.*;

import java.net.*;



public class ClientT{

public static void main(String[] args){

String welcome,response;

Client client;

client=new Client("100.100.100.1",8001); // #1

DataInputStream keyboard=new DataInputStream(System.in);

boolean Bye=false;

while(!Bye){

try{

welcome=client.in.readLine();

System.out.println(" Server says: "+welcome);

client.out.println(keyboard.readLine());

}catch(IOException e){

System.out.println("\n The talk is CLOSED !");

System.exit(1);

}

}

try{

Thread.sleep(200);

}catch(Exception e){

System.out.println("It is a bug !");

}

}

}

class Client{

public DataInputStream in;

public PrintStream out;

private Socket client;

public Client(String host,int port){

try{

client=new Socket(host,port);

System.out.println("\n

***************************************************");

System.out.println("\n @(#)Net Applecation

Version 1.00 97/12/30 ");

System.out.println(" Copyright (c) 1997

(Lui DongBing) All Rights Reserved.");

System.out.println("\n

**************************************************");

System.out.println("\n Client socket:"+client);

System.out.println("\n Client is ready ! \n");

out=new PrintStream(client.getOutputStream());

in=new DataInputStream(client.getInputStream());

}catch(IOException e){

System.out.println("\n IOException !\n"+e);

System.exit(1);

}

}

}

 

100.100.100.1为本机地址

本文地址:http://com.8s8s.com/it/it18784.htm