如果是Frame
具体说的话,只能添加一项,所以使用方法
def contents_= (c: Component) : Unit
你应该很好。在 REPL 中试试这个(一次一行,这样你就可以看到发生了什么):
import scala.swing._
val f = new Frame { contents = new Label("Hi") }
f.visible = true
f.contents = new Label("Hey there")
如果您正在使用旨在包含多个项目的东西,例如 a BoxPanel
, contents
is aBuffer
所以您可以添加到它并从中删除:
val bp = new BoxPanel(Orientation.Vertical)
val (label1,label2) = (new Label("Hi"), new Label("there"))
bp.contents += label1
bp.contents += label2
f.contents = bp // Now you see "Hi" "there" stacked
bp.contents -= label1
f.pack // "Hi" disappears--need pack not repaint to fix layout
bp.contents += label1
f.pack // "Hi" is back, but at the end
如果您有其他类似 a 的东西Component
正在扩展,那么您的工作就是contents
用缓冲区覆盖或以其他方式修改它(或从SequentialContainer
J-16 所说的继承)。