【问题标题】:Swingbuilder - application location centeringSwingbuilder - 应用程序位置居中
【发布时间】:2010-04-26 14:38:26
【问题描述】:

我正在使用 Griffon->SwingBuilder 创建一个应用程序。我希望能够使应用程序在桌面上居中。

我知道我们可以在创建应用程序时提供“位置:[x,y]”参数。反正有没有访问桌面属性来计算中心?

【问题讨论】:

    标签: java griffon swingbuilder


    【解决方案1】:

    由于各种原因,您不能内联。这是居中的一种方法

    import java.awt.*
    import groovy.swing.*
    
    sb = new SwingBuilder()
    sb.build {
      f = frame(pack:true) {
          label "<html>" + (("This is a very long label."*3) + "<BR>")*5
      }
      Point cp = GraphicsEnvironment.localGraphicsEnvironment.centerPoint
      f.location = new Point((int)(cp.x - f.width), (int) (cp.y - f.height))
      f.show()
    }
    

    您不能在属性中设置它的原因是,在评估属性时,尚未在任何地方创建或存储子节点。一种替代方法是将其设置为子内容块的一部分:

      frame(show:true) 
      {
          label "<html>" + (("This is a very long label."*3) + "<BR>")*5
          current.pack()
          Point cp = GraphicsEnvironment.localGraphicsEnvironment.centerPoint
          current.location = new Point((int)(cp.x -current.width/2), (int)(cp.y - current.height/2))
      }
    

    (current 是包含节点的元变量)。

    【讨论】:

    • 非常感谢 - 我尝试了第二个选项,它似乎工作得很好。
    【解决方案2】:

    Swing 的一个特点是,它会记住最后的位置和大小(如果可调整大小)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-26
      • 2018-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      • 2011-05-13
      • 2010-10-21
      相关资源
      最近更新 更多