
2007年12月12日
一,做一個最簡單的Applet:
1,編譯JAVA類:
{TOMCAT_HOME} = D:\tomcat6
假設在D:\tomcat6\webapps\testweb\test目錄中新建hello目錄,在hello目錄中新建java類HelloWorld.java:
package hello;
import java.awt.*;
import java.applet.*;
//必須繼承Appelet類
public class HelloWorld extends Applet {
public void paint(Graphics g )
{
//向外輸出的字符串
g.drawString("Hello HelloWorldd!",5,35);
}
}
在CMD命令行中編譯該類:
C:\Documents and Settings\Administrator>d:
D:>cd D:\tomcat6\webapps\testweb\test
D:>javac hello\HelloWorld.java
2,在網頁中嵌入Applet:
在D:\tomcat6\webapps\testweb\test目錄中新建testHello.html文件:
<HTML>
<TITLE>HelloWorld! Applet</TITLE>
<APPLET
codebase="."
CODE="hello.HelloWorld.class"
name="HelloWorld"
WIDTH=200
HEIGHT=100
ALIGN = middle
>
</APPLET>
</HTML>
3,運行查看Applet:
appletviewer是一個專門用來運行查看applet的工具,直接在命令行上敲打就可以
在CMD命令行中
>d:
>cd D:\tomcat6\webapps\testweb\test
>appletviewer testHello.html
如果啟動了TOMCAT,也可以在瀏覽器中瀏覽,不過有些瀏覽器不支持,需要JAVA插件才可以看到效果,比如遨游,我是在IE6.0中運行可以看到。
二,Applet進一步處理:
如果想把網頁中的Applet標簽轉換成<Object>或者<Embed>形式,需要做以下工作,
4,將hello目錄打包:
>d:
>cd D:\tomcat6\webapps\testweb\test
>jar -cvf hello.jar ./hello
5,使用htmlconverter工具來轉換:
1)下載htmlconverter==>http://java.sun.com/products/plugin/1.3/docs/html_converter.html
2)下載后是一個壓縮文件,解壓縮后成為一個jar包,假設該jar包在D:\tomcat6\webapps\testweb\test下
>d:
>cd D:\tomcat6\webapps\testweb\test
>java -jar htmlconv1_3.jar helloworld.jsp (注:不清楚htmlconverter怎么用的,可以敲命令查看幫助:java -jar htmlconv1_3.jar -help)
此時helloworld.jsp中的代碼將被轉換成如下樣式:
<HTML>
<TITLE>HelloWorld! Applet</TITLE>
<!--"CONVERTED_APPLET"-->
<!-- HTML CONVERTER -->
<OBJECT classid="clsid:E19F9331-3110-11d4-991C-005004D3B3DB"
WIDTH = 200 HEIGHT = 100 NAME = "HelloWorld" ALIGN = middle codebase="http://java.sun.com/products/plugin/1.3.0_02/jinstall-130_02-win32.cab#Version=1,3,0,2">
<PARAM NAME = CODE VALUE = "hello.HelloWorld.class" >
<PARAM NAME = CODEBASE VALUE = "." >
<PARAM NAME = ARCHIVE VALUE = "HelloWorld.jar" >
<PARAM NAME = NAME VALUE = "HelloWorld" >
<PARAM NAME="type" VALUE="application/x-java-applet;jpi-version=1.3.0_02">
<PARAM NAME="scriptable" VALUE="false">
<COMMENT>
<EMBED type="application/x-java-applet;jpi-version=1.3.0_02" CODE = "hello.HelloWorld.class" CODEBASE = "." ARCHIVE = "HelloWorld.jar" NAME = "HelloWorld" WIDTH = 200 HEIGHT = 100 ALIGN = middle scriptable=false pluginspage="http://java.sun.com/products/plugin/1.3.0_02/plugin-install.html"><NOEMBED>
</NOEMBED>
</EMBED>
</COMMENT>
</OBJECT>
<!--
<APPLET CODE = "hello.HelloWorld.class" CODEBASE = "." ARCHIVE = "HelloWorld.jar" WIDTH = 200 HEIGHT = 100 NAME = "HelloWorld" ALIGN = middle>
</APPLET>
-->
<!--"END_CONVERTED_APPLET"-->
</HTML>
這樣轉換就已經完成,此時再用appletviewer去查看,效果和前面一致
posted @
2007-12-12 13:19 詩語江南 閱讀(4710) |
評論 (0) |
編輯 收藏

2007年10月5日
import java.util.*;
/*
* @author 詩語江南
* @function 統計字符串中的重復部分并整理輸出,
* 我用了兩種方法來做.
*/
public class StrShowTimes{
public static void main(String[] r){
String str = "帥哥,美女,帥哥,野獸,美女,帥哥";
Map s1 = strTimesWithMap(str);
Set keys = s1.keySet();
Iterator it = keys.iterator();
while(it.hasNext()){
String key = (String)it.next();
int value = (Integer) s1.get(key);
System.out.print(key + ": " + value+ ", ");
}
System.out.println();
strTimesWithArray(str);
}
//使用HashMap的方法,該方法比較簡單
public static Map strTimesWithMap(String str){
//key: 子字符串 String , value: 重復次數 Integer
Map strMap = new HashMap();
String[] strArr = str.split(",");
for(int i =0; i< strArr.length ; i++){
String key = strArr[i] ;
if(strMap.containsKey(key)){
int value = (Integer) strMap.get(key);
strMap.put(key,++value);
}else{
strMap.put(key,1);
}
}
return strMap;
}
//使用雙數組的方法,一個字符串數組存字符串
//一個整形數組存與字符串數組對應位置上的字符串出現的次數
public static void strTimesWithArray(String str){
String[] tempArr = str.split(",");
int i , end = 0 , len = tempArr.length;
String[] strArr = new String[len];
int[] intArr = new int[len];
boolean isChange ;
for(i = 0 ; i < len ; i++){
isChange = false;
for(int j = 0 ; j < end ; j++){
if(tempArr[i].equals(strArr[j])){
intArr[j] = intArr[j] +1 ;
isChange = true;
break;
}
}
if(isChange) continue;
strArr[end] = tempArr[i];
intArr[end++] = 1;
}
for(i = 0 ; i < end ; i++){
System.out.print(strArr[i] + ": " + intArr[i] + " ");
}
}
}
posted @
2007-10-05 13:36 詩語江南 閱讀(1678) |
評論 (3) |
編輯 收藏