本文介绍了斯卡拉弹出菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Scala 中显示弹出窗口?我有一个后门",但对我来说似乎很丑:

How do I cause a popup to get shown in Scala? I have a "backdoor" but it seems pretty ugly to me:

val item = new MenuItem(new Action("Say Hello") {
  def apply = println("Hello World");
})
//SO FAR SO GOOD, NOW FOR THE UGLY BIT!
val popup = new javax.swing.JPopupMenu
popup.add(item.peer)
popup.setVisible(true)

推荐答案

你做的很好,但是如果你想隐藏对等调用,你可以创建自己的类:

What you are doing is fine, but if you'd like to hide the peer call you could create your own class:

class PopupMenu extends Component
{
  override lazy val peer : JPopupMenu = new JPopupMenu

  def add(item:MenuItem) : Unit = { peer.add(item.peer) }
  def setVisible(visible:Boolean) : Unit = { peer.setVisible(visible) }
  /* Create any other peer methods here */
}

那么你可以这样使用它:

Then you can use it like this:

val item = new MenuItem(new Action("Say Hello") {
  def apply = println("Hello World");
})

val popup = new PopupMenu
popup.add(item)
popup.setVisible(true)

作为替代方案,您可以尝试 SQUIB(Scala 的 Quirky User Interface Builder).有了SQUIB,上面的代码就变成了:

As an alternative, you could try SQUIB (Scala's Quirky User Interface Builder). With SQUIB, the above code becomes:

popup(
  contents(
    menuitem(
      'text -> "Say Hello",
      actionPerformed(
        println("Hello World!")
      )
    )
  )
).setVisible(true)

这篇关于斯卡拉弹出菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:32