我們將使用Flash Builder 4.5創建Flex應用程式。讓我們從一個簡單的應用程式開始。
Step 1 – Create Project
第一步是使用Flash Builder IDE創建一個簡單的Flex項目。使用選項File>New>Flex project啓動項目嚮導。現在使用嚮導窗口將您的項目命名爲HelloWorld,如下所示−
選擇應用程式類型Web(在Adobe Flash Player中運行)。但是,如果未選中此選項,則保留其他默認值,然後單擊「完成」按鈕。成功創建項目後,您的項目資源管理器中將包含以下內容−
以下是對所有重要文件夾的簡要描述;
Folder | Location |
---|---|
table table-bordered |
原始碼(mxml/as類)文件。 |
bin-debug | 這是輸出部分,它表示實際的可部署web應用程式。 History文件夾包含Flex應用程式歷史管理的支持文件。 framework_xxx.swf,flex應用程式應該使用flex framework文件。 HelloWorld.html,flex應用程式的包裝器/宿主html文件。 HelloWorld.swf,我們基於flex的應用程式。 playerProductInstall.swf,flash播放器快速安裝程序。 spark_xxx.swf,spark組件支持庫。 js,負責在HelloWorld.html中加載HelloWorld.swf的JavaScript。它檢查flash播放器版本並將初始化參數傳遞給HelloWorld.swf文件。 textLayout xxx.swf,文本組件支持庫。 |
html-template |
這表示可配置的web應用程式。Flash Builder將文件從html模板編譯到bin調試文件夾。 History文件夾包含Flex應用程式歷史管理的支持文件。 index.template.html,flex應用程式的包裝器/宿主html文件,具有Flash Builder特定配置的占位符。在生成期間編譯到bin debug文件夾中的HelloWorld.html。 playerProductInstall.swf,flash player express安裝程序將在生成期間複製到bin debug文件夾。 js,負責在HelloWorld.html中加載HelloWorld.swf的JavaScript。它檢查flash播放器版本,並將初始化參數傳遞給HelloWorld。swf文件在生成期間被複製到bindebug文件夾。 |
Step 2 – Create External CSS File
在HTML template文件夾中爲包裝HTML頁面創建CSS文件styles.CSS。
html, body { height:100%; } body { margin:0; padding:0; overflow:auto; text-align:center; } object:focus { outline:none; } #flashContent { display:none; } .pluginHeader { font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#9b1204; text-decoration:none; font-weight:bold; } .pluginInstallText { font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#000000; line-height:18px; font-style:normal; } .pluginText { font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#000000; line-height:18px; font-style:normal; }
Step 3 – Modify Wrapper HTML page template
修改HTML template文件夾中的包裝HTML頁面模板index.template.HTML。Flash Builder將創建一個默認的包裝HTML頁面模板HTML template/index.template.HTML,該模板將編譯爲HelloWorld.HTML。
此文件包含Flash Builder在編譯過程中替換的占位符。例如,flash播放器版本、應用程式名稱等。
如果沒有安裝flash插件,讓我們修改這個文件以顯示自定義消息。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml" lang = "en" xml:lang = "en"> <head> <title>${title}</title> <meta name = "google" value = "notranslate" /> <meta http-equiv = "Content-Type" content = "text/html; charset = utf-8" /> <link rel = "stylesheet" href = "styles.css" type = "text/css"></link> <link rel = "stylesheet" type = "text/css" href = "history/history.css" /> <script type = "text/javascript" table table-bordered = "history/history.js"> </script> <script type = "text/javascript" table table-bordered = "swfobject.js"></script> <script type = "text/javascript"> // For version detection, set to min. required Flash Player version, //or 0 (or 0.0.0), for no version detection. var swfVersionStr = "${version_major}.${version_minor}.${version_revision}"; // To use express install, set to playerProductInstall.swf, //otherwise the empty string. var xiSwfUrlStr = "${expressInstallSwf}"; var flashvars = {}; var params = {}; params.quality = "high"; params.bgcolor = "${bgcolor}"; params.allowscriptaccess = "sameDomain"; params.allowfullscreen = "true"; var attributes = {}; attributes.id = "${application}"; attributes.name = "${application}"; attributes.align = "middle"; swfobject.embedSWF ( "${swf}.swf", "flashContent", "${width}", "${height}", swfVersionStr, xiSwfUrlStr, flashvars, params, attributes); // JavaScript enabled so display the flashContent div in case //it is not replaced with a swf object. swfobject.createCSS("#flashContent", "display:block;text-align:left;"); </script> </head> <body> <div id = "flashContent"> <p style = "margin:100px;"> <table width = "700" cellpadding = "10" cellspacing = "2" border = "0"> <tr> <td class = "pluginHeader">Flash Player Required</td> </tr> <tr> <td class = "pluginText">The Adobe Flash Player version 10.2.0 or greater is required.</td> </tr> <tr> <td class = "pluginInstallText" align = "left"> <table border = "0" width = "100%"> <tr class = "pluginInstallText" > <td>Click here to download and install Adobe Flash Player:</td> <td> </td> <td align = "right"> <script type = "text/javascript"> var pageHost = ((document.location.protocol == "https:") ? "https://" : "http://"); document.write("<a target = '_blank'" +" href = 'http://get.adobe.com/flashplayer/'><" +"img style = 'border-style: none' table table-bordered = '" +pageHost +"www.adobe.com/images/shared/download_buttons/get_flash_player.gif'" +" alt = 'Get Adobe Flash player' /></a>" ); </script> </td> </tr> </table> </tr> </table> </p> </div> <noscript> <object classid = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width = "${width}" height = "${height}" id = "${application}"> <param name = "movie" value = "${swf}.swf" /> <param name = "quality" value = "high" /> <param name = "bgcolor" value = "${bgcolor}" /> <param name = "allowScriptAccess" value = "sameDomain" /> <param name = "allowFullScreen" value = "true" /> <!--[if !IE]>--> <object type = "application/x-shockwave-flash" data = "${swf}.swf" width = "${width}" height = "${height}"> <param name = "quality" value = "high" /> <param name = "bgcolor" value = "${bgcolor}" /> <param name = "allowScriptAccess" value = "sameDomain" /> <param name = "allowFullScreen" value = "true" /> <!--<![endif]--> <!--[if gte IE 6]>--> <p> <p style = "margin:100px;"> <table width = "700" cellpadding = "10" cellspacing = "2" border = "0"> <tr> <td class = "pluginHeader">Flash Player Required</td> </tr> <tr> <td class = "pluginText">The Adobe Flash Player version 10.2.0 or greater is required.</td> </tr> <tr> <td class = "pluginInstallText" align = "left"> <table border = "0" width = "100%"> <tr class = "pluginInstallText" > <td>Click here to download and install Adobe Flash Player:</td> <td> </td> <td align = "right"> <script type = "text/javascript"> var pageHost = ((document.location.protocol == "https:") ? "https://" : "http://"); document.write("<a target = '_blank'" +" href = 'http://get.adobe.com/flashplayer/'><" +"img style = 'border-style: none' table table-bordered = '" +pageHost +"www.adobe.com/images/shared/download_buttons/get_flash_player.gif'" +" alt = 'Get Adobe Flash player' /></a>" ); </script> </td> </tr> </table> </tr> </table> </p> </p> <!--<![endif]--> <p style = "margin:100px;"> <table width = "700" cellpadding = "10" cellspacing = "2" border = "0"> <tr><td class = "pluginHeader">Flash Player Required</td></tr> <tr><td class = "pluginText">The Adobe Flash Player version 10.2.0 or greater is required.</td></tr> <tr> <td class = "pluginInstallText" align = "left"> <table border = "0" width = "100%"> <tr class = "pluginInstallText" > <td>Click here to download and install Adobe Flash Player:</td> <td> </td> <td align = "right"> <script type = "text/javascript"> var pageHost = ((document.location.protocol == "https:") ? "https://" : "http://"); document.write("<a target = '_blank'" +" href = 'http://get.adobe.com/flashplayer/'><" +"img style = 'border-style: none' table table-bordered = '" +pageHost +"www.adobe.com/images/shared/download_buttons/get_flash_player.gif'" +" alt = 'Get Adobe Flash player' /></a>" ); </script> </td> </tr> </table> </td> </tr> </table> </p> <!--[if !IE]>--> </object> <!--<![endif]--> </object> </noscript> </body> </html>
Step 4 – Create Internal CSS file
/* CSS file */ @namespace s "library://ns.adobe.com/flex/spark"; @namespace mx "library://ns.adobe.com/flex/mx"; .heading { fontFamily: Arial, Helvetica, sans-serif; fontSize: 17px; color: #9b1204; textDecoration:none; fontWeight:normal; } .button { fontWeight: bold; } .container { cornerRadius :10; horizontalCenter :0; borderColor: #777777; verticalCenter:0; backgroundColor: #efefef; }
Step 5 – Modify Entry Level Class
<?xml version = "1.0" encoding = "utf-8"?> <s:Application xmlns:fx = "http://ns.adobe.com/mxml/2009" xmlns:s = "library://ns.adobe.com/flex/spark" xmlns:mx = "library://ns.adobe.com/flex/mx width = "100%" height = "100%" minWidth = "500" minHeight = "500" initialize = "application_initializeHandler(event)"> <fx:Style source = "/com/tutorialspoint/client/Style.css" /> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.FlexEvent; protected function btnClickMe_clickHandler(event:MouseEvent):void { Alert.show("Hello World!"); } protected function application_initializeHandler(event:FlexEvent):void { lblHeader.text = "My Hello World Application"; } ]]> </fx:Script> <s:BorderContainer width = "500" height = "500" id = "mainContainer" styleName = "container"> <s:VGroup width = "100%" height = "100%" gap = "50" horizontalAlign = "center" verticalAlign = "middle"> <s:Label id = "lblHeader" fontSize = "40" color = "0x777777" styleName = "heading" /> <s:Button label = "Click Me!" id = "btnClickMe" click = "btnClickMe_clickHandler(event)" styleName = "button" /> </s:VGroup> </s:BorderContainer> </s:Application>
您可以在同一個源目錄中創建更多的mxml或actionscript文件,以定義新的應用程式或定義幫助程序例程。
Step 6 – Build Application
默認情況下,Flash Builder已選中「自動生成」。如果有任何錯誤,只需檢查問題視圖。完成更改後,將不會看到任何錯誤。
Step 7 – Run Application
現在點擊run application菜單並選擇HelloWorld來運行應用程式。
如果一切正常,您必須看到瀏覽器彈出,應用程式啓動,並運行。如果您的應用程式一切正常,它將產生以下結果:[聯機試用]
因爲,你正在flash player中運行你的應用程式,那麼它需要爲你的瀏覽器安裝flashplayer插件。只需按照螢幕上的說明安裝插件。如果您已經爲您的瀏覽器設置了Flash Player插件,那麼您應該能夠看到以下輸出;
祝賀 你!您已經使用Flex實現了第一個應用程式。