JavaFX Label 類(lèi)支持HTML內(nèi)容。使用Label您可以使用HTML和CSS創(chuàng)建樣式文本和圖像,非常類(lèi)似于典型的Web應(yīng)用。此外,通過(guò)使用JavaFX嵌入表達(dá)式,您可以在Swing應(yīng)用中象Web頁(yè)面作者使用類(lèi)似 JSTL 或 Velocity 工具一樣創(chuàng)建動(dòng)態(tài)的HTML內(nèi)容。
考慮以下虛擬購(gòu)物車(chē)示例:
import javafx.ui.*; class Item { ???????????? attribute id: String ;???????????? attribute productId: String ;???????????? attribute description: String ;???????????? attribute inStock: Boolean ;???????????? attribute quantity: Number ;???????????? attribute listPrice: Number ;???????????? attribute totalCost: Number ;???????? } ???????? attribute Item. totalCost = bind quantity*listPrice; ???????? class Cart { ???????????? attribute items: Item*;???????????? attribute subTotal: Number ;???????? } ???????? operation sumItems ( itemList:Item* ) { ???????????? var result = 0.00 ;???????????? for ( item in itemList ) { ???????????????? result += item. totalCost ;???????????? } ???????????? return result;???????? } ???????? attribute Cart. subTotal = bind sumItems ( items ) ; ???????? var cart = Cart { ???????????? items:???????????? [ Item { ???????????????? id: "UGLY" ???????????????? productId: "D100" ???????????????? description: "BullDog" ???????????????? inStock: true ???????????????? quantity: 1 ???????????????? listPrice: 97.50 ???????????? } ,???????????? Item { ???????????????? id: "BITES" ???????????????? productId: "D101" ???????????????? description: "Pit Bull" ???????????????? inStock: true ???????????????? quantity: 1 ???????????????? listPrice: 127.50 ???????????? } ] ???????? } ;???????? Frame { ????????????? content: Label { ???????????????? text: bind ??????????????????? "<html>??????????????????????? <h2 align='center'>Shopping Cart</h2>??????????????????????? <table align='center' border='0' bgcolor='#008800' cellspacing='2' cellpadding='5'>?????????????????????????? <tr bgcolor='#cccccc'>????????????????????????????? <td><b>Item ID</b></td>????????????????????????????? <td><b>Product ID</b></td>????????????????????????????? <td><b>Description</b></td>????????????????????????????? <td><b>In Stock?</b></td>????????????????????????????? <td><b>Quantity</b></td>????????????????????????????? <td><b>List Price</b></td>????????????????????????????? <td><b>Total Cost</b></td>????????????????????????????? <td> </td>??????????????????????????? </tr> ??????????????????????????? {????????????????????????????? if (sizeof cart.items == 0)????????????????????????????? then " <tr bgcolor= '#FFFF88' ><td colspan= '8' ><b>Your cart is empty.</b></td></tr> "????????????????????????????? else foreach (item in cart.items)????????????????????????????????? " <tr bgcolor= '#FFFF88' >?????????????????????????????????? <td> { item. id } </td>?????????????????????????????????? <td> { item. productId } </td>?????????????????????????????????? <td> { item. description } </td>?????????????????????????????????? <td> { if item. inStock then "Yes" else "No" } </td>?????????????????????????????????? <td> { item. quantity } </td>?????????????????????????????????? <td align= 'right' > { item. listPrice } </td>?????????????????????????????????? <td align= 'right' > { item. totalCost } </td>?????????????????????????????????? <td> </td>?????????????????????????????????? </tr> "??????????????????????????? } ??????????????????????????? <tr bgcolor='#FFFF88'>???????????????????????????????? <td colspan='7' align='right'>??????????????????????????????????? <b>Sub Total: ${cart.subTotal}</b>??????????????????????????????? </td>??????????????????????????????? <td> </td>???????????????????????????? </tr>????????????????????????? </table>?????????????????????? </html>" ???????????????? } ???????????????? visible: true ???????? }
運(yùn)行以上程序,顯示如下:
如果您編程刪除購(gòu)物車(chē)內(nèi)容:
delete cart.
items
;
您將看到如下內(nèi)容:
在以上示例中,內(nèi)嵌的JavaFX表達(dá)式(粗體顯示)動(dòng)態(tài)創(chuàng)建HTML表格列和表格單元的內(nèi)容。當(dāng)這些表達(dá)式依賴(lài)的對(duì)象有變化時(shí),Label的HTML內(nèi)容將自動(dòng)更新。
以上示例還非常有趣,因?yàn)槠溲菔玖耸褂帽磉_(dá)式定義屬性值的用法。Item類(lèi)的totalCost屬性和Cart類(lèi)的subTotal屬性被綁定為表達(dá)式以計(jì)算它們的值。任何時(shí)候這些表達(dá)式的依賴(lài)對(duì)象發(fā)生變化,屬性值將被自動(dòng)重新計(jì)算并更新??紤]在電子表格中,某些單元格包含指向其他單元格的表達(dá)式,當(dāng)您在這些其他單元格輸入數(shù)據(jù),包含依賴(lài)它們的表達(dá)式的單元格值也被自動(dòng)更新了。
HTML中的圖像
JavaFX Label類(lèi)實(shí)際上封裝了一個(gè)特殊的JEditPane,該 JEditorPane 使用一個(gè)支持用Java類(lèi)轉(zhuǎn)載器從JAR文件中載入圖像的共享圖像緩存。因此,您可以就像普通的文件URL一樣使用HTML的<img>元素引用您的應(yīng)用的圖像資源包。
超鏈接
Label類(lèi)同樣支持HTML超鏈接,內(nèi)嵌一個(gè)特殊的URL給HTML<a>元素的href屬性。
這樣的URL使用JavaFX #操作符創(chuàng)建,該操作符生成一個(gè)字符串化對(duì)象引用(Stringified Object Reference)指向后續(xù)可以被JavaFX復(fù)引用的操作對(duì)象。?操作符,例如:
var a = 20 ;var b = #a; assert b instanceof String ; // 通過(guò) var c = ( Number ) ?b; assert a == c;?? // 通過(guò)
Label類(lèi)的HTML顯示器認(rèn)識(shí)諸如HTML的<a href=url>這樣的URL,使用URL來(lái)響應(yīng)元素的鼠標(biāo)點(diǎn)擊,并且如果URL值指向一個(gè)函數(shù)或操作的話(huà),它可以調(diào)用該函數(shù)或操作。
例如,以下是使用帶超鏈接標(biāo)簽代替按鈕的前面按鈕點(diǎn)擊示例的重寫(xiě)版本:
import javafx.ui.*; import java.lang.System; class ButtonClickModel { attribute numClicks: Number ; } var model = new ButtonClickModel ( ) ; var win = Frame { ?? width: 300 ?? height: 200 ?? menubar: MenuBar { ???? menus: Menu { ?????? text: "File" ?????? mnemonic: F?????? items: MenuItem { ???????? text: "Exit" ???????? mnemonic: X???????? accelerator: { ?????????? modifier: ALT?????????? keyStroke: F4??????????? } ???????? action: operation ( ) { ?????????? System . exit ( 0 ) ;??????????? } ???????? } ?????? } ??? } ?? content: GridPanel { ???? border: EmptyBorder { top: 30 left: 30 bottom: 30 right: 30 ????? } ???? rows: 2 ???? columns: 1 ???? vgap: 10 ???? cells:???? [ Label { ??????? text: bind??????? "<html>?????????? <a href='{#(operation() {model.numClicks++;})}'>???????????? I'm a hyperlink!?????????? </a>???????? </html>" ??????? } ,????? Label { ??????? text: bind "Number of button clicks: {model.numClicks}" ?????? } ] ?? } ?? visible: true } ;
以上示例中粗體的表達(dá)式創(chuàng)建一個(gè)新的遞增model的numClicks屬性的操作。使用#操作符生成后續(xù)將被鍵入到HTML標(biāo)記中的指向該操作的URL。
運(yùn)行該程序,顯示如下:
點(diǎn)擊超鏈接兩次后,顯示如下:
?
JavaFX發(fā)現(xiàn)之旅 JavaFX Script With Eclipse 入門(mén)(第三部分)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
