首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Scala-Swing当对话框访问父内容时,父内容被清除

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

Stack Overflow用户
提问于 2016-04-01 03:50:12
回答 1查看 119关注 0票数 2

我正在开发一个使用Scala-swing前端的应用程序。我有一个填充的MainFrame,并且工作正常。我也有一个运行良好的对话框。但是当我从对话框访问父框架时,框架的内容就清晰了。MemuBar仍然在那里。

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

我正在使用"org.scala-lang.modules“% "scala-swing_2.11”% "1.0.2“

有谁有什么想法吗?

谢谢!

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

代码语言:javascript
复制
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)
}
EN

回答 1

Stack Overflow用户

发布于 2016-04-04 22:22:21

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

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

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

代码语言:javascript
复制
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)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36342456

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档