在 scala.swing 中,如何从容器中删除组件?

发布于 2024-11-04 04:08:27 字数 415 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘然心甜 2024-11-11 04:08:27

如果您具体讨论的是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")

如果您使用的东西旨在包含多个项目,例如 BoxPanelcontents< /code> 是一个 Buffer,因此您可以向其中添加或从中删除:

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

如果您有其他东西,例如要扩展的 Component,那么您的工作就是使用缓冲区覆盖 contents 或使用其他方式修改它(或如 J-16 所说从 SequentialContainer 继承)。

If you're talking about Frame specifically, you can only add one item, so use the method

def contents_= (c: Component) : Unit

and you should be good. Try this out in the REPL (one line at a time so you can see what's going on):

import scala.swing._
val f = new Frame { contents = new Label("Hi") }
f.visible = true
f.contents = new Label("Hey there")

If you're using something that is intended to have multiple items like a BoxPanel, contents is a Buffer so you can add to it and remove from it:

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

If you have something else like a Component that you're extending, it is your job to override contents with a buffer or have some other way of modifying it (or inherit from SequentialContainer as J-16 said).

櫻之舞 2024-11-11 04:08:27

Container 中的 contents 是一个 Seq[]
您需要一个 SequentialContainer 来执行该删除方法。

contents in Container was a Seq[];
you need a SequentialContainer for that remove method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文