这次我们学习Label标签(源码以上传):
代码如下:
import javafx.ui.*;
import java.lang.System;
class Item {//定义类Item
attribute id: String;//属性id 字符串类型
attribute productId: String; //属性productId 字符串类型 --产品
attribute description: String;//属性description 字符串类型 -- 描述
attribute inStock: Boolean;//属性inStock 布尔类型 -- 库存
attribute quantity: Number;//属性quantity 数字类型 --数量
attribute listPrice: Number;//属性listPrice 数字类型世纪末 --价格列表
attribute totalCost: Number;//属性totalCost 数字类型 --合计成本
}
attribute Item.totalCost = bind quantity*listPrice;//为属性Item里的totalCost绑定quantity*listPrice
class Cart {
attribute items: Item*;//定义属性items是Item类型并且是多个
attribute subTotal: Number;//属性subTotal --合计
}
operation sumItems(itemList:Item*) {//操作方法
var result = 0.00;//保存结果
for (item in itemList) {
result += item.totalCost;//用一定for循环进行合计成本累加
}
return result;//返回结果
}
attribute Cart.subTotal = bind sumItems(items);//为属性Cart里的subTotal绑定items
var cart = 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>
{ //类似jsp一堆混乱代码,不方便阅读,书写
//html里夹java代码,java代码夹html代码
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>"
}
centerOnScreen:true//居中显示
visible: true//设为可见
}
图一.
图二.
图三.
图四.
运行结果: