如何使用SuperMixin创建Scala swing包装器类? [英] How to create Scala swing wrapper classes with SuperMixin?

查看:84
本文介绍了如何使用SuperMixin创建Scala swing包装器类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从该线程的答案中了解以下类的工作方式: Scala弹出菜单

I'm trying to understand how the following class works taken from an answer from this thread: Scala Popup Menu

由于线程很旧,我决定只开始一个新问题.我是Java背景下的Scala初学者,我想知道此类如何工作.我读到一个与类同名的对象就像一个具有单例对象的类吗?我不确定这是否适合实现包装器..(为什么我们需要该对象?)

Since the thread is pretty old I decided to just start a new question. I'm new to Scala with a Java background and I'm wondering how this class works. I read that an object with the same name as a class is like a class with a singleton object? I'm not sure how this fits in to achieving the wrapper though.. (why do we need the object?)

SuperMixin特性到底能做什么? API说:此特征用于将某些调用从对等方重定向到包装器,然后再返回.对公开可以通过重写进行自定义的方法很有用."不能很好地向初学者解释.

And what exactly does the SuperMixin trait do? The API says "This trait is used to redirect certain calls from the peer to the wrapper and back. Useful to expose methods that can be customized by overriding." which doesn't do a very good job of explaining to a beginner.

如果有人可以帮助向初学者说明这个类和对象(在我看来,是神奇的)如何为我提供JPopupMenu的包装器类并让我调用使popupMenu出现在其中的show方法,我将不胜感激.屏幕上..好像也可以设置其内容(contents + =一些scala.swing.menuItem),而无需在下面的类中定义它?

I would really appreciate it if someone can help explain to a beginner how this class and object (it seems to me, magically) get me a wrapper class for the JPopupMenu and lets me call the show method which makes a popupMenu appear on the screen.. and it also seems like I can set its content (contents+= some scala.swing.menuItem) without it being defined in the class below?

import javax.swing.JPopupMenu
import scala.swing.{ Component, MenuItem }
import scala.swing.SequentialContainer.Wrapper

object PopupMenu {
  private[PopupMenu] trait JPopupMenuMixin { def popupMenuWrapper: PopupMenu }
}

class PopupMenu extends Component with Wrapper {

  override lazy val peer: JPopupMenu = new JPopupMenu with PopupMenu.JPopupMenuMixin with SuperMixin {
    def popupMenuWrapper = PopupMenu.this
  }

  def show(invoker: Component, x: Int, y: Int): Unit = peer.show(invoker.peer, x, y)

  /* Create any other peer methods here */
}

推荐答案

用于PopupMenu伴侣对象在这里没有任何特定目的,只是用作辅助特征的名称空间JPopupMenuMixin.然后可以通过将其设置为private[PopupMenu]来隐藏"此特征,因此只有类PopupMenu及其伴随对象才知道该特征.

The companion object for PopupMenu doesn't serve any specific purpose here except functioning as a name space for the auxiliary trait JPopupMenuMixin. This trait can then be "hidden" by making it private[PopupMenu], so it is only known by class PopupMenu and its companion object.

坦率地说,我看不到该特征的目的.它定义了指向外部Scala Swing组件的方法popupMenuWrapper,但是根本没有使用该方法.因此,一个不太令人困惑的版本将很简单:

Frankly, I don't see the purpose of that trait. It defines a method popupMenuWrapper pointing to the outer Scala Swing component, but that method is not used at all. So a less confusing version would be simply:

import scala.swing._
import javax.swing.JPopupMenu

class PopupMenu extends Component with SequentialContainer.Wrapper {
  override lazy val peer: JPopupMenu = new JPopupMenu with SuperMixin

  def show(invoker: Component, x: Int, y: Int): Unit = peer.show(invoker.peer, x, y)
}

测试:

val pop = new PopupMenu {
  contents += new MenuItem("Foo")
}
lazy val but: Button = Button("Test") {
  pop.show(but, 0, 0)
}
val f = new Frame {
  contents = but
  pack().centerOnScreen()
  open()
}

包装器唯一需要做的就是扩展scala.swing.Component并使用基础javax.swing组件覆盖peer值. mixin with SuperMixin 覆盖该组件的几种方法(例如paintComponent),以便将其转发到外部包装器组件.就是这样.

The only thing necessary for a wrapper is to extend scala.swing.Component and override the peer value with the underlying javax.swing component. The mixin with SuperMixin overrides a few methods of that component, such as paintComponent, in order to forward them to the outer wrapper component. That's all.

包装器在SequentialContainer.Wrapper中混合,允许contents +=操作添加菜单项.

The wrapper mixes in SequentialContainer.Wrapper which allows for the contents += operation to add the menu items.

这篇关于如何使用SuperMixin创建Scala swing包装器类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆