【问题标题】:Can't change swing window location dynamically无法动态更改摆动窗口位置
【发布时间】:2015-11-18 23:01:16
【问题描述】:

我想更改 scala-swing 窗口的位置。 我发现初始化时设置位置可以正常工作,但不能动态更改位置

import java.awt.Point

import scala.swing._
import scala.swing.event.ButtonClicked

object TestWindow extends SimpleSwingApplication {
  def top = new MainFrame {
    contents = new BoxPanel(Orientation.Vertical) {
      contents += new Button("Change location") {
        reactions += {
          case e: ButtonClicked => {
            println("change location")
            top.location = new Point(200, 100)//doesn't work
            println("change location end")
          }
        }
      }
    }
    location = new Point(100, 50) //works correctly
  }
}

我该怎么做?

【问题讨论】:

    标签: scala-swing


    【解决方案1】:

    可以使用以下语句实现:

    this.peer.getRootPane.getParent.setLocation(new Point(200, 100))
    

    这里this.peer 是对java JButton 的引用,getRootPane 为您提供按钮的占位符,然后在这种特殊情况下getParent 获取您的MainFrame 对象(如果层次结构可能需要使用多个getParent更大)。我认为通过使用 top.location,您正在尝试创建一个新的 MainFrame,这可能会被 API 禁止。

    【讨论】:

      猜你喜欢
      • 2010-12-05
      • 2010-12-05
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多