Scala Swing当对话框访问父对象时,父对象内容将被清除

Scala Swing当对话框访问父对象时,父对象内容将被清除,swing,scala,Swing,Scala,我正在开发一个带有Scala swing前端的应用程序。我有一个大型机,填充和工作良好。我有一个对话,工作也很好。但是,当我从对话框访问父帧时,该帧的内容将被清除。记忆棒还在那里 我只需要将对话框放在框架的中心,所以我只传递了一个点(在尝试正确操作之后),甚至这也会导致问题。我可以设置在对话框中创建的位置点,但我无法访问框架来设置。这个我真的不明白;我在帧中创建点并将其发送到对话框,这很好,但将对话框位置设置为它会清除帧 我使用的是“org.scala-lang.modules”%“scala-

我正在开发一个带有Scala swing前端的应用程序。我有一个大型机,填充和工作良好。我有一个对话,工作也很好。但是,当我从对话框访问父帧时,该帧的内容将被清除。记忆棒还在那里

我只需要将对话框放在框架的中心,所以我只传递了一个点(在尝试正确操作之后),甚至这也会导致问题。我可以设置在对话框中创建的位置点,但我无法访问框架来设置。这个我真的不明白;我在帧中创建点并将其发送到对话框,这很好,但将对话框位置设置为它会清除帧

我使用的是“org.scala-lang.modules”%“scala-swing_2.11”%“1.0.2”

有人有什么想法吗

谢谢

另一方面,这个演示代码工作得很好,所以它不是那么简单

package hack

import scala.swing.{Action, BorderPanel, Button, Dialog, Dimension, FlowPanel, Frame, MainFrame, Menu, MenuBar, MenuItem, SimpleSwingApplication, TabbedPane}

/**
  * Created by bday on 3/31/16.<br>
  * <br>
  * FrameClearingDialog will do something useful I'm sure
  */
class FrameClearingDialog (parent: Frame) {
  val dialog = new Dialog
  dialog.contents = new FlowPanel() {
    preferredSize = new Dimension(500,500)
  }
  dialog.open()
  dialog.setLocationRelativeTo(parent)
}

class Parent extends SimpleSwingApplication {
  override def top: Frame = new MainFrame {
    title = "Hack "
    preferredSize = new Dimension(1000,1000)
    menuBar = new MenuBar() {
      contents += new Menu("Menu") {
        contents += new MenuItem(Action("Show Dialog") {
          createAndShowDialog
        })
      }
    }
    val panel = new BorderPanel() {
      layout(new Button() {text="button"}) = BorderPanel.Position.North
      layout(new Button() {text="button"}) = BorderPanel.Position.Center
      layout(new Button() {text="button"}) = BorderPanel.Position.South
    }
    contents = new TabbedPane() {
        pages += new TabbedPane.Page("Page", panel)
    }
  }

  def createAndShowDialog = {
    new FrameClearingDialog(top)
  }
}

object Starter extends App {
  val demo = new Parent
  demo.main(args)
}
包黑客
导入scala.swing.{Action,BorderPanel,Button,Dialog,Dimension,FlowPanel,Frame,MainFrame,Menu,MenuBar,MenuItem,SimpleSwingApplication,TabbedPane}
/**
*由bday于2016年3月31日创建。
*
*我相信FrameClearingDialog会做一些有用的事情 */ 类FrameClearingDialog(父级:Frame){ val对话框=新建对话框 dialog.contents=新建流程面板(){ 首选尺寸=新尺寸(500500) } dialog.open() 对话框.setLocationRelativeTo(父级) } 类父级扩展SimpleSwingApplication{ 覆盖def top:Frame=新主机{ title=“黑客” 首选尺寸=新尺寸(10001000) 菜单栏=新菜单栏(){ 内容+=新菜单(“菜单”){ 内容+=新菜单项(操作(“显示对话框”){ createAndShowDialog }) } } val面板=新边框面板(){ 布局(新按钮(){text=“Button”})=BorderPanel.Position.North 布局(新按钮(){text=“Button”})=BorderPanel.Position.Center 布局(新按钮(){text=“Button”})=BorderPanel.Position.South } contents=新选项卡窗格(){ pages+=新选项卡窗格。页面(“页面”,面板) } } def createAndShowDialog={ 新建FrameClearingDialog(顶部) } } 对象启动程序扩展应用程序{ val demo=新父级 demo.main(args) }
这不是答案,但它确实解释了这个问题,足以理解和避免它

问题似乎在于范围。如果内容是在大型机构造函数内部创建的,那么它会在子级调用中生存,如果在外部创建,则不会。Swing有时会做一些奇怪的事情,我现在不打算在这上面花更多的时间

如果将“映射”的创建移到大型机中,则此示例将正常工作


包黑客
导入scala.swing.{Action、BorderPanel、BoxPanel、Button、Dialog、Dimension、FlowPanel、Frame、MainFrame、Menu、MenuBar、MenuItem、Orientation、Panel、Point、RichWindow、SimpleSwingApplication、TabbedPane}
/**
*由bday于2016年3月31日创建。
*
*Utils肯定会有用的 */ 对象Utils{ def findCenter(窗口:RichWindow)={ 新点(window.location.x+window.size.width/2,window.location.y+window.size.height/2) } def centerMe(父级:RichWindow,子级:RichWindow)={ val parentCenter=findCenter(父级) val childCenter=findCenter(子) 新点(parentCenter.x-childCenter.x,parentCenter.y-childCenter.y) } } /** *由bday于2016年3月31日创建。
*
*我相信FrameClearingDialog会做一些有用的事情 */ 类FrameClearingDialog(父级:Frame){ val对话框=新建对话框 dialog.contents=新建流程面板(){ preferredSize=新尺寸(500500) } dialog.location=Utils.centerMe(父级,对话框) dialog.open() } 类父级扩展SimpleSwingApplication{ val映射={ var map=map.empty[Int,Panel] 用于(x)createPanel(x) } 地图 } 覆盖def top:Frame=新主机{ title=“黑客” 首选尺寸=新尺寸(10001000) 菜单栏=新菜单栏(){ 内容+=新菜单(“菜单”){ 内容+=新菜单项(操作(“显示对话框”){ createAndShowDialog }) } } contents=新选项卡窗格(){
对于(x我解决了我的问题,但我还不明白为什么这是一个问题。为了允许我调用选项卡式窗格中的页面,我用属于页面内容的面板制作了一个地图。然后我循环了这个,并用内容加载了页面。这可以工作,但当对话框访问它时,内容丢失。使用相同的方法创建内容方法转换为val,然后转换为页面,映射可以,但将映射保留为var。我更新了示例,但仍然有效。因此,我仍然没有找到真正的问题。当我在主窗口上关闭()并最终打开()时,我也会遇到同样的问题再一次。不幸的是,唯一的解决办法是,正如您在自己的回答中所显示的那样……在大型机构造函数中创建所有内容。大型机构造函数出现了一些奇怪的情况……它在重新打开时会被调用两次。
package hack

import scala.swing.{Action, BorderPanel, BoxPanel, Button, Dialog, Dimension, FlowPanel, Frame, MainFrame, Menu, MenuBar, MenuItem, Orientation, Panel, Point, RichWindow, SimpleSwingApplication, TabbedPane}

/**
  * Created by bday on 3/31/16.<br>
  * <br>
  * Utils will do something useful I'm sure
  */
object Utils {

  def findCenter(window: RichWindow) = {
    new Point(window.location.x + window.size.width/2, window.location.y + window.size.height/2)
  }

  def centerMe(parent: RichWindow, child: RichWindow) = {
    val parentCenter = findCenter(parent)
    val childCenter = findCenter(child)
    new Point(parentCenter.x - childCenter.x, parentCenter.y - childCenter.y)
  }
}

/**
  * Created by bday on 3/31/16.<br>
  * <br>
  * FrameClearingDialog will do something useful I'm sure
  */
class FrameClearingDialog (parent: Frame) {
  val dialog = new Dialog
  dialog.contents = new FlowPanel() {
    preferredSize = new Dimension(500, 500)
  }
  dialog.location = Utils.centerMe(parent, dialog)
  dialog.open()
}

class Parent extends SimpleSwingApplication {

    val map = {
      var map = Map.empty[Int, Panel]
      for (x <- 1 to 5) {
        map += x -> createPanel(x)
      }
      map
    }

  override def top: Frame = new MainFrame {
    title = "Hack "
    preferredSize = new Dimension(1000,1000)
    menuBar = new MenuBar() {
      contents += new Menu("Menu") {
        contents += new MenuItem(Action("Show Dialog") {
          createAndShowDialog
        })
      }
    }
    contents = new TabbedPane() {
      for (x <- 1 to 5) {
        pages += new TabbedPane.Page(s"Page $x", map(x))
      }
    }
  }

  def createPanel(x: Int) = {
    new BoxPanel(Orientation.Vertical) {
      contents += new Button() {text=s"button $x"}
    }
  }

  def createAndShowDialog = {
    new FrameClearingDialog(top)
  }
}

object Starter extends App {
  val demo = new Parent
  demo.main(args)
}