Scala-Swing当Dialog访问父级时,父级内容将清除

时间:2016-03-31 19:50:12

标签: swing scala

我正在使用Scala-swing前端开发一个应用程序。我有一个填充并运行良好的MainFrame。我有一个效果很好的对话框。但是当我从对话框中访问父框架时,框架的内容会清除。 MemuBar仍在那里。

我只需要将对话框放在框架上,所以我只是通过了一个Point(在尝试正确之后),甚至导致问题。我可以设置在对话框中创建的位置点我无法访问框架来执行此操作。而这我真的没有得到;我在框架中创建了点并将其发送到对话框,这很好,但是将对话框位置设置为清除框架。

我正在使用“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)
}

1 个答案:

答案 0 :(得分:1)

这不是答案,但它确实解释了足以理解和避免它的问题。

问题似乎是范围。如果内容是在MainFrame构造函数内创建的,那么它将从子项调用中幸存下来,如果在外部创建则不会。 Swing有时会做一些奇怪的事情,我现在不会花更多的时间在这上面。

如果将“map”的创建移动到MainFrame中,则此示例将正常工作。

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)
}