Scala Swing - 带操作的按钮

发布于 2024-12-01 21:32:31 字数 904 浏览 1 评论 0原文

我在视图中创建了一个带有标题和图标的按钮。

object playButton extends Button("play") {
  icon = new ImageIcon(getClass.getResource("/Play.gif"))
  verticalTextPosition = Alignment.Bottom
  horizontalTextPosition = Alignment.Center
}

现在我想在控制器中添加一些操作。

view.playButton.action = Action(view.playButton.text) { 
  //...
}

问题是,此操作会覆盖按钮图标。所以...我尝试过:

view.playButton.action = Action(view.playButton.text) { 
  icon = view.playButton.icon
}

编译器说:

[info] Compiling main sources...
[error] .../Controller.scala:11: not found: value icon
[error]     icon = view.playButton.icon
[error]     ^
[error] one error found

我做错了什么?文档中的操作具有图标字段的设置器: http://www .scala-lang.org/api/current/scala/swing/Action.html

I've created a button with title and icon in my view.

object playButton extends Button("play") {
  icon = new ImageIcon(getClass.getResource("/Play.gif"))
  verticalTextPosition = Alignment.Bottom
  horizontalTextPosition = Alignment.Center
}

Now I want to add it some action in the controller.

view.playButton.action = Action(view.playButton.text) { 
  //...
}

The problem is, that this action overrides buttons icon. So... i tried:

view.playButton.action = Action(view.playButton.text) { 
  icon = view.playButton.icon
}

Compiler says:

[info] Compiling main sources...
[error] .../Controller.scala:11: not found: value icon
[error]     icon = view.playButton.icon
[error]     ^
[error] one error found

What am I doing wrong? Action in documentation has this setter for icon field: http://www.scala-lang.org/api/current/scala/swing/Action.html.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

携君以终年 2024-12-08 21:32:31

看看 source for scala.swing.Action

在配套的 object 中:

def apply(title: String)(body: =>Unit) = new Action(title) { 
  def apply() { body }
}

换句话说,为了方便您,它们采用了块(您放置的位置) icon = ...) 并将其作为该操作的事件处理程序。

您真正想做的是子类:

new Action("Hello") {
    icon = ...

    def apply() = ...
}

这似乎没有记录。

Look at the source for scala.swing.Action

In the companion object:

def apply(title: String)(body: =>Unit) = new Action(title) { 
  def apply() { body }
}

In other words, as a convenience to you, they take the block (where you put icon = ...) and make that be the event handler for the Action.

What you actually want to do is subclass:

new Action("Hello") {
    icon = ...

    def apply() = ...
}

This does not appear to be documented.

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