我正在使用 Griffon->SwingBuilder 创建一个应用程序。我希望能够将应用程序置于桌面中心。
我知道我们可以在创建应用程序时提供“位置:[x,y]”参数。无论如何访问桌面属性来计算中心?
最佳答案
由于各种原因,您不能内联。这是居中的一种方法
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()
}
你不能在属性中设置它的原因是当属性被评估时,一个子节点还没有被创建或存储在任何地方。一种替代方法是将其设置为子内容 block 的一部分:
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 是包含节点的元变量)。
关于java - Swingbuilder - 应用程序位置居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2714314/