在 scala.swing 中,如何从容器中删除组件?
在 scala.swing 中,我可以像这样向容器添加组件:
new Frame {
contents += label
}
但有时我想清除容器的内容并用新组件替换它们。根据文档,我应该能够执行以下操作:
frame.contents.remove(0)
或者
frame.contents.clear
但是这些都不能编译(“清除/删除不是 Seq[scala.swing.Component] 的成员”)。
我该如何解决这个问题?另外,初始化后我似乎无法调用 frame.contents += blah
。如果是这样,如何将新组件添加到容器中?
In scala.swing, I can add a component to a container like so:
new Frame {
contents += label
}
but sometimes I'd like to clear the contents of a container and replace them with new components. Based on the docs, I should be able to do:
frame.contents.remove(0)
or
frame.contents.clear
but neither of those compile ("clear/remove is not a member of Seq[scala.swing.Component]").
How can I resolve this? Also, it seems that I can't call frame.contents += blah
after intialization. If this is so, how do I add a new component to a Container?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您具体讨论的是
Frame
,则只能添加一项,因此使用该方法应该会很好。在 REPL 中尝试一下(一次一行,这样您就可以看到发生了什么):
如果您使用的东西旨在包含多个项目,例如
BoxPanel
、contents< /code> 是一个
Buffer
,因此您可以向其中添加或从中删除:如果您有其他东西,例如要扩展的
Component
,那么您的工作就是使用缓冲区覆盖contents
或使用其他方式修改它(或如 J-16 所说从SequentialContainer
继承)。If you're talking about
Frame
specifically, you can only add one item, so use the methodand you should be good. Try this out in the REPL (one line at a time so you can see what's going on):
If you're using something that is intended to have multiple items like a
BoxPanel
,contents
is aBuffer
so you can add to it and remove from it:If you have something else like a
Component
that you're extending, it is your job to overridecontents
with a buffer or have some other way of modifying it (or inherit fromSequentialContainer
as J-16 said).Container
中的contents
是一个Seq[]
;您需要一个
SequentialContainer
来执行该删除方法。contents
inContainer
was aSeq[]
;you need a
SequentialContainer
for that remove method.