【问题标题】:Scala Swing - button with actionScala Swing - 带有动作的按钮
【发布时间】:2011-08-28 01:11:43
【问题描述】:

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

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

【问题讨论】:

    标签: swing scala button icons action


    【解决方案1】:

    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() = ...
    }
    

    这似乎没有记录。

    【讨论】:

      猜你喜欢
      • 2013-04-17
      • 1970-01-01
      • 2017-02-18
      • 1970-01-01
      • 2014-07-17
      • 2021-06-04
      • 2017-03-04
      • 2012-02-11
      • 2011-12-06
      相关资源
      最近更新 更多