
- Adobe Flex 教程
- Flex - 首頁
- Flex - 概述
- Flex - 環境
- Flex - 應用程式
- Flex - 建立應用程式
- Flex - 部署應用程式
- Flex - 生命週期階段
- Flex - 使用 CSS 樣式化
- Flex - 使用皮膚樣式化
- Flex - 資料繫結
- Flex - 基本控制元件
- Flex - 表單控制元件
- Flex - 複雜控制元件
- Flex - 佈局面板
- Flex - 視覺效果
- Flex - 事件處理
- Flex - 自定義控制元件
- Flex - RPC 服務
- Flex - FlexUnit 整合
- Flex - 除錯應用程式
- Flex - 國際化
- Flex - 列印支援
- Adobe Flex 資源
- Flex - 快速指南
- Flex - 有用資源
- Flex - 討論
Flex - 建立應用程式
我們將使用 Flash Builder 4.5 來建立 Flex 應用程式。讓我們從一個簡單的HelloWorld應用程式開始。
步驟 1 – 建立專案
第一步是使用 Flash Builder IDE 建立一個簡單的 Flex 專案。使用選項檔案 > 新建 > Flex 專案啟動專案嚮導。現在使用嚮導視窗將您的專案命名為HelloWorld,如下所示:

選擇應用程式型別Web(在 Adobe Flash Player 中執行)。但是,如果未選中此選項,則保留其他預設值,然後單擊“完成”按鈕。專案成功建立後,您的專案資源管理器中將包含以下內容:

以下是所有重要資料夾的簡要說明:
資料夾 | 位置 |
---|---|
表格 table-bordered |
原始碼 (mxml/as 類) 檔案。 我們建立了 com/tutorialspoint/client 資料夾結構,其中包含負責客戶端 UI 顯示的客戶端特定 Java 類。 |
bin-debug | 這是輸出部分,它代表實際的可部署 Web 應用程式。 History 資料夾包含 Flex 應用程式歷史管理的支援檔案。 framework_xxx.swf,Flex 應用程式應使用 Flex 框架檔案。 HelloWorld.html,Flex 應用程式的包裝器/宿主 HTML 檔案。 HelloWorld.swf,我們的基於 Flex 的應用程式。 playerProductInstall.swf,Flash Player 快速安裝程式。 spark_xxx.swf,Spark 元件支援庫。 swfobject.js,負責在 HelloWorld.html 中載入 HelloWorld.swf 的 JavaScript。它檢查 Flash Player 版本並將初始化引數傳遞給 HelloWorld.swf 檔案。 textLayout_xxx.swf,文字元件支援庫。 |
html-template |
這代表可配置的 Web 應用程式。Flash Builder 將 html-template 中的檔案編譯到 bin-debug 資料夾。 History 資料夾包含 Flex 應用程式歷史管理的支援檔案。 index.template.html,Flex 應用程式的包裝器/宿主 HTML 檔案,其中包含 Flash Builder 特定配置的佔位符。在構建過程中編譯到 bin-debug 資料夾中的 HelloWorld.html。 playerProductInstall.swf,Flash Player 快速安裝程式在構建過程中被複制到 bin-debug 資料夾。 swfobject.js,負責在 HelloWorld.html 中載入 HelloWorld.swf 的 JavaScript。它檢查 Flash Player 版本並將初始化引數傳遞給 HelloWorld.swf 檔案,在構建過程中被複制到 bin-debug 資料夾。 |
步驟 2 – 建立外部 CSS 檔案
為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; }
步驟 3 – 修改包裝器 HTML 頁面模板
修改html-template資料夾中的包裝器 HTML 頁面模板index.template.html。Flash Builder 將建立一個預設的包裝器 HTML 頁面模板html-template/index.template.html,該模板將編譯為 HelloWorld.html。
此檔案包含 Flash Builder 在編譯過程中替換的佔位符。例如,Flash Player 版本、應用程式名稱等。
讓我們修改此檔案以在未安裝 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>
步驟 4 – 建立內部 CSS 檔案
為table tablebordered/com/tutorialspoint資料夾中的HelloWorld.mxml建立一個 CSS 檔案Style.css。Flex 為其 UI 控制元件提供的 CSS 樣式與 HTML UI 控制元件的 CSS 樣式類似。
/* 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; }
步驟 5 – 修改入口級類
Flash Builder 將建立一個預設的 mxml 檔案table tablebordered/com.tutorialspoint/HelloWorld.mxml,該檔案具有應用程式的根標籤<application>容器。讓我們修改此檔案以顯示“Hello,World!”:
<?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 檔案來定義新的應用程式或定義輔助例程。
步驟 6 – 構建應用程式
Flash Builder 預設情況下選中了自動構建。如果存在任何錯誤,請檢查問題檢視。完成更改後,您將不會看到任何錯誤。
步驟 7 – 執行應用程式
現在單擊執行應用程式選單並選擇HelloWorld應用程式以執行該應用程式。

如果一切正常,您應該會看到瀏覽器彈出視窗,應用程式啟動並執行。如果您的應用程式一切正常,它將產生以下結果:[線上試用]
因為您正在 Flash Player 中執行應用程式,所以它需要為您的瀏覽器安裝 Flash Player 外掛。只需按照螢幕上的說明安裝外掛即可。如果您已經為瀏覽器設定了 Flash Player 外掛,那麼您應該能夠看到以下輸出:

恭喜!您已使用Flex實現了您的第一個應用程式。