這里不但是設(shè)置字體格式的地方,還是設(shè)置所有環(huán)境參數(shù)的地方,各種參數(shù)都是通過這里而改變的。各種功能參數(shù)的改變都比較的具體,比如說VJ的自動(dòng)補(bǔ)償功能,就是選中Text Editor中的Per Language中的Auto list members。相信大家也自己看的懂,這里就不羅嗦了。
在New選項(xiàng)中的Application中,有三個(gè)選項(xiàng),第一個(gè)是Windows Application,這就是使用微軟自帶類庫(kù)進(jìn)行WFC程序的界面設(shè)計(jì)應(yīng)用程序,第二個(gè)選項(xiàng)是 Console Application 這是DOS下的程序設(shè)計(jì),沒有界面要求,只要完成核心代碼要求就可以了,不必設(shè)計(jì)界面,當(dāng)然也用不著WFC類庫(kù)了。第三個(gè)選項(xiàng)為"Application Wizard"這個(gè)選項(xiàng)功能就是應(yīng)用程序設(shè)計(jì)向?qū)Я耍灰阏罩f的做,一步一步往下走,就可以了,用這個(gè)向?qū)гO(shè)計(jì)出來的程序,具有很好的界面,一說到界面,當(dāng)然就也用了WFC,這也不在我的討論只列了。給大家舉一個(gè)DOS下的應(yīng)用程序的例子,在上圖中,我們選擇Console Application,填好名字與路徑之后,點(diǎn)擊 確定按鈕,VJ6就會(huì)自動(dòng)幫你生成一些格式化的代碼,如下: /** * This class can take a variable number of parameters on the command * line. Program execution begins with the main() method. The class * constrUCtor is not invoked unless an object of type ´Class1´ * created in the main() method. */ public class Class1 { /** * The main entry point for the application. * * @param args Array of parameters passed to the application * via the command line. */ public static void main (String[] args) { // TODO: Add initialization code here } } 它前面的"/**"與"*/"之間的說明性語(yǔ)句一大堆,告訴你一些基本的內(nèi)容,基本上不用理會(huì),在主函數(shù)"main"中,有一個(gè)TODO,那就是我們要加入代碼的地方了,我們就加入一個(gè)例子說明一下,在TODO下一行,我們鍵入一行“System. out. println ("Hello!");”(請(qǐng)注重大小寫),假如你學(xué)過C或者JAVA的話,那就知道是什么意思了,這個(gè)程序是一個(gè)最基本的HELLO程序,在輸入的時(shí)候要注重到它的自動(dòng)補(bǔ)償功能,當(dāng)你敲完System后,再敲一個(gè)點(diǎn)時(shí),后面就會(huì)出現(xiàn)一個(gè)下拉選擇框,你再敲一個(gè)"o"的時(shí)候,選擇項(xiàng)就自動(dòng)停在了"out"選項(xiàng)上面。這時(shí)候只要按一下空格,就可以看到"out"三個(gè)字母已經(jīng)自動(dòng)輸入了,當(dāng)你再敲一個(gè)點(diǎn)的時(shí)候,同樣又會(huì)出現(xiàn)一個(gè)下拉選擇框,按下"p"字母后,選擇項(xiàng)就停留在了以"p"開頭的選項(xiàng)上,按下方向鍵的向下鍵,找到"println"按下空格,就會(huì)自動(dòng)補(bǔ)償輸入了。同理,當(dāng)你此時(shí)按下"("時(shí),就有一個(gè)選擇項(xiàng)提示,1 of 10,后面是方法,表示這是10種方法中的第一種方法,按下方向鍵的向下或者向上鍵,就可以看到不同方法的提示了,每種提示中都有參數(shù)的個(gè)數(shù),參數(shù)的類型等提示,非常的方便快捷,使你的輸入一般不會(huì)出現(xiàn)語(yǔ)法錯(cuò)誤。(這要求你已經(jīng)打開了自動(dòng)補(bǔ)償功能,假如沒有打開,請(qǐng)參看前面打開的方法)假如你不小心輸入錯(cuò)了,則在最下面的浮動(dòng)窗口"Task List"中,會(huì)用紅色的下化線顯示你的輸入有問題,或者是藍(lán)色的警告。
/** * This class can take a variable number of parameters on the command * line. Program execution begins with the main() method. The class * constructor is not invoked unless an object of type ´Form1´ is * created in the main() method. */ public class Form1 extends Form { public Form1() { // Required for Visual J++ Form Designer support initForm();
// TODO: Add any constructor code after initForm call }
/** * Form1 overrides dispose so it can clean up the * component list. */ public void dispose() { super.dispose(); components.dispose(); }
/** * NOTE: The following code is required by the Visual J++ form * designer. It can be modified using the form editor. Do not * modify it using the code editor. */ Container components = new Container(); Edit edit1 = new Edit(); Edit edit2 = new Edit(); Button button1 = new Button();
/** * The main entry point for the application. * * @param args Array of parameters passed to the application * via the command line. */ public static void main(String args[]) { Application.run(new Form1()); } }
1 選擇新建菜單“new”,然后選擇“new”下面的選項(xiàng):Applet on HTML,設(shè)置好名稱與路徑之后,按下確定按鈕,系統(tǒng)將自動(dòng)幫我們生成一兩個(gè)文件,一個(gè)是Applet的初始代碼文件,另外一個(gè)就是HTML網(wǎng)頁(yè)文件了。如下圖:
這個(gè)窗口里面有三個(gè)選項(xiàng)了,假如你用過Frontpage,那么你將對(duì)這個(gè)非常的熟悉。這里就像Frontpage一樣,是一個(gè)網(wǎng)頁(yè)編輯器,工具箱中也有你編輯網(wǎng)頁(yè)所要的控件。不用多解釋,看看Source(源代碼),打開后,有的可能顯示的是該Applet的內(nèi)容。而有的可能顯示的是代碼,這與VJ6的初始設(shè)定有關(guān),在Applet上面點(diǎn)擊鼠標(biāo)右鍵,選擇“Always view as text”后,就可以看到代碼了,代碼如下:
<applet code=Applet1.class name=Applet1 width=320 height=200 VIEWASTEXT> <param name=label value="This string was passed from the HTML host."> <param name=background value="008080"> <param name=foreground value="FFFFFF"> </applet>
Font ft; //文字字體對(duì)象實(shí)例 String theString, font, style; //移動(dòng)的字符串、字體類型及樣式 int size, speed; //文字的大小,移動(dòng)速度 int xSet, Max, Min;//文字所在位置的x坐標(biāo),右邊界和左邊界
定義好變量后,就要像給出的源代碼一樣,加入一些初始的代碼了。
private final String param_string="string"; private final String param_font="font"; private final String param_style="style"; private final String param_size="size"; private final String param_speed="speed";
public void paint(Graphics g) { int ySet=getSize().height ;//yset為輸出字符串的y坐標(biāo)變量 ySet=(ySet-size)/2; g.setFont (ft); g.setColor (Color.red); g.drawString (theString,xSet,ySet+40); //+40是用來調(diào)整輸出時(shí)的高度用的。 xSet--; if(xSet }
public Thread TextMoveRunnable=null; public void start() { if(TextMoveRunnable==null) { TextMoveRunnable=new Thread (this); TextMoveRunnable.start (); } } public void run() { while(true) { try{ repaint();Thread.sleep (speed); }catch(InterruptedException e) {} } } public void stop() { if (TextMoveRunnable!=null) { TextMoveRunnable.stop(); TextMoveRunnable=null; } }
這部分代碼應(yīng)該加在與initForm()并列的地方,可以在之前,也可以在之后。在加完這些代碼之后,程序的大體就完成了。但假如你此時(shí)編譯的話,肯定會(huì)出現(xiàn)錯(cuò)誤,在Task List上會(huì)顯示出來,并且錯(cuò)誤為“Class ´Thread´ doesn´t have a constructor that matches ´Thread(Applet1)´ (J0082)”這是因?yàn)榧舆M(jìn)去的代碼涉及到了線程,而主程序不知道,沒有相應(yīng)的接口,(J0082)為錯(cuò)誤的序號(hào),查找相應(yīng)的工具,有這個(gè)錯(cuò)誤的具體解釋。要解決這個(gè)錯(cuò)誤,只要在主類后面加上一個(gè)接口就可以了。如下:
public class Applet1 extends Applet implements Runnable
/** * This class reads PARAM tags from its HTML host page and sets * the color and label properties of the applet. Program execution * begins with the init() method. */ public class Applet1 extends Applet implements Runnable { /** * The entry point for the applet. */ Font ft; //文字字體對(duì)象實(shí)例 String theString, font, style; //移動(dòng)的字符串、字體類型及樣式 int size, speed; //文字的大小,移動(dòng)速度 int xSet, Max, Min;//文字每次移動(dòng)的位移量,右邊界和左邊界
public void init() { initForm();
usePageParams();
// TODO: Add any constructor code after initForm call. }
private final String labelParam = "label"; private final String backgroundParam = "background"; private final String foregroundParam = "foreground"; private final String param_string="string"; private final String param_font="font"; private final String param_style="style"; private final String param_size="size"; private final String param_speed="speed";
/** * Reads parameters from the applet´s HTML host and sets applet * properties. */ private void usePageParams() { final String defaultLabel = "Default label"; final String defaultBackground = "C0C0C0"; final String defaultForeground = "000000"; String labelValue; String backgroundValue; String foregroundValue;
/** * Read the , * , * and tags from * the applet´s HTML host. */ labelValue = getParameter(labelParam); backgroundValue = getParameter(backgroundParam); foregroundValue = getParameter(foregroundParam);
if ((labelValue == null) (backgroundValue == null) (foregroundValue == null)) { /** * There was something wrong with the HTML host tags. * Generate default values. */ labelValue = defaultLabel; backgroundValue = defaultBackground; foregroundValue = defaultForeground; }
/** * Converts a string formatted as "rrggbb" to an awt.Color object */ private Color stringToColor(String paramValue) { int red; int green; int blue;
red = (Integer.decode("0x" + paramValue.substring(0,2))).intValue(); green = (Integer.decode("0x" + paramValue.substring(2,4))).intValue(); blue = (Integer.decode("0x" + paramValue.substring(4,6))).intValue();
return new Color(red,green,blue); }
/** * External interface used by design tools to show properties of an applet. */ public String[][] getParameterInfo() { String[][] info = { { labelParam, "String", "Label string to be displayed" }, { backgroundParam, "String", "Background color, format "rrggbb"" }, { foregroundParam, "String", "Foreground color, format "rrggbb"" }, }; return info; }
Label label1 = new Label();
/** * Intializes values for the applet and its components */ void initForm() { this.setBackground(Color.lightGray); this.setForeground(Color.black); label1.setText("label1"); this.setLayout(new BorderLayout()); this.add("North",label1); }
public void paint(Graphics g) { int ySet=getSize().height ; ySet=(ySet-size)/2; g.setFont (ft); g.setColor (Color.red); g.drawString (theString,xSet,ySet+40); //+40是用來調(diào)整輸出時(shí)的高度用的。 xSet--; if(xSet }
public Thread TextMoveRunnable=null; public void start() { if(TextMoveRunnable==null) { TextMoveRunnable=new Thread (this); TextMoveRunnable.start (); } } public void run() { while(true) { try{ repaint();Thread.sleep (speed); } catch(InterruptedException e) {} } } public void stop() { if (TextMoveRunnable!=null) { TextMoveRunnable.stop(); TextMoveRunnable=null; } } }
5 修改HTML內(nèi)的代碼:
<applet code=Applet1.class name=movetext width=400 height=100 VIEWASTEXT> <param name=label value="This string was passed from the HTML host."> <param name=background value="008080"> <param name=foreground value="FFFFFF"> <param name=string value="你好, 這是一個(gè)例子!"> <param name=font value="宋體"> <param name=style value=BOLD> <param name=size value=40> <param name=speed value=20> </applet> 當(dāng)你設(shè)置好這些后,應(yīng)該是沒有什么問題了吧。此時(shí)按下F5鍵,在彈出的默認(rèn)瀏覽器內(nèi),你將看到這個(gè)例子的效果了。怎么樣,沒有騙你吧!好了,到此,Applet的編寫與編譯的全部過程你應(yīng)該把握了吧。