【问题标题】:SimpleSwingApplication created a seemingly infinite amount of identical windowsSimpleSwingApplication 创建了看似无限数量的相同窗口
【发布时间】:2016-10-23 20:20:38
【问题描述】:

所以我在 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)

    }
}

.

object CViewer {
  def main(args: Array[String]): Unit = {
    val coreWindow = CViewerMainWindow
    coreWindow.top
  }
}

我曾希望它会创建一个普通的窗口。相反,我得到了这个:

【问题讨论】:

    标签: swing scala scala-swing


    【解决方案1】:

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

    def top = new MainFrame {
      listenTo(top)
    }
    

    也就是说,top 正在调用 top 正在调用 top... 以下应该可以工作:

    def top = new MainFrame {
      listenTo(this)
    }
    

    但更好更安全的方法是禁止多次实例化主框架:

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

    【讨论】:

    • 谢谢,这很有意义。
    猜你喜欢
    • 2012-07-21
    • 2012-05-18
    • 2011-06-30
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多