SimpleSwingApplication创建了看似无限多的相同窗口

SimpleSwingApplication创建了看似无限多的相同窗口,swing,scala,scala-swing,Swing,Scala,Scala Swing,因此,我在scala中有以下简单的程序: object CViewerMainWindow extends SimpleSwingApplication { var i = 0 def top = new MainFrame { title = "Hello, World!" preferredSize = new Dimension(320, 240) // maximize visible = true

因此,我在scala中有以下简单的程序:

object CViewerMainWindow extends SimpleSwingApplication {
    var i = 0
    def top = new MainFrame {
        title = "Hello, World!"
        preferredSize = new Dimension(320, 240)
        // maximize
        visible = true
        contents = new Label("Here is the contents!")
        listenTo(top)
        reactions += {
            case UIElementResized(source) => println(source.size)

    }
}

我曾希望它能创造一个普通的窗口。相反,我得到的是:


您正在创建一个无限循环:

def top = new MainFrame {
  listenTo(top)
}
也就是说,
top
正在调用
top
正在调用
top
。。。以下方面应起作用:

def top = new MainFrame {
  listenTo(this)
}
但更好、更安全的方法是禁止主框架多次实例化:

lazy val top = new MainFrame {
  listenTo(this)
}

谢谢,这很有道理。
lazy val top = new MainFrame {
  listenTo(this)
}