reeCell。
接下来,让我们重建一个Swing教程中的示例(GenealogyExample),它显示了某人的后代或者父辈情况。
当我们运行这个示例后,程序将显示以下:
如果在Tree中选择某人并点击某个单选按钮中,那么这个被选择的人将成为Tree的根。Tree将根据选择将此人的父辈或者后代显示在其子节点中。
下面是示例代码。其中与Tree有关的代码以粗体显示。TreeCell具有一个selected属性(Boolean类型),它决定了自身是否被选择。与此同时,如果你通过程序将一个Boolean值赋值给这个属性的话,相应的TreeCell将依照selected属性值被选择或者取消选择。
在示例中,由于家谱是一个递归的数据结构,于是我们需要使用一个能够被递归调用的表达式来定义TreeCell的cells属性。请注意:在这里我们使用了bind lazy操作符而不是在初始化cells属性中使用的bind,因为它标识了lazy式求值。这意味着直到它左侧表达式第一次被访问到时,其右侧表达式才被求值。因此,对descendantTree()和ancestorTree()函数的递归调用并非马上执行,而是直到你展开Tree中的某个节点,Tree要求访问子节点的cells时才被执行。
侮秘僥楼JavaFX重云囂冱(中?Swing殻會埀) ---(和)(5)
扮寂:2011-04-19 cleverpig
class GeneologyModel {
attribute people: Person*;
attribute selectedPerson: Person;
attribute showDescendants: Boolean;
}
class Person {
attribute selected: Boolean;
attribute father: Person;
attribute mother: Person;
attribute children: Person*;
attribute name: String;
}
// By defining these triggers I can populate the model
// by just assigning the mother and father attributes of a Person
trigger on Person.father = father {
insert this into father.children;
}
trigger on Person.mother = mother {
insert this into mother.children;
}
// Create and populate the model
var model = GeneologyModel {
var jack = Person {
selected: true
name: "Jack (great-granddaddy)"
}
var jean = Person {
name: "Jean (great-granny)"
}
var albert = Person {
name: "Albert (great-granddaddy)"
}
var rae = Person {
name: "Rae (great-granny)"
}
var paul = Person {
name: "Paul (great-granddaddy)"
}
var josie = Person {
name: "Josie (great-granny)"
}
var peter = Person {
father: jack
mother: jean
name: "Peter (grandpa)"
}
var zoe = Person {
father: jack
mother: jean
name: "Zoe (grandma)"
}
var simon = Person {
father: jack
mother: jean
name: "Simon (grandpa)"
}
var james = Person {
father: jack
|