3

scala.swing.BoxPanel,但似乎没有抓住重点,因为没有工厂javax.swing.Box方法、、、createHorizontalStrut和的等价物。这些方法也返回 的实例,因此不能提交给.createHorizontalGluecreateVerticalStrutcreateVerticalGluejava.awt.Componentscala.swing.Component.wrap

是否有任何简单的解决方法来创建间距和胶水scala.swing.BoxPanel?如果没有,是否有任何现有的开源库包装了 的功能javax.swing.Box

4

2 回答 2

5

我一直使用以下胶水和支柱(你可以在 REPL 中运行它来测试):

import swing._
import Swing._ // object with many handy functions and implicits

val panel = new BoxPanel(Orientation.Vertical) {
  contents += new Label("header")
  contents += VStrut(10)
  contents += new Label("aoeu")
  contents += VGlue
  contents += new Label("footer")
}

new Frame { contents = panel; visible = true }

HGlue 和 HStrut 也有一些方法。

于 2012-08-05T18:26:18.450 回答
1

Swing 库中缺少各种功能。

这是我对胶水和支柱的解决方案:

import javax.{swing => jsw}
class HorzPanel extends BoxPanel(Orientation.Horizontal) {
  def glue = { peer.add(jsw.Box.createHorizontalGlue); this }
  def strut(n: Int) = { peer.add(jsw.Box.createHorizontalStrut(n)); this }
}
object HorzPanel {
  def apply(cs: Component*) = new HorzPanel { contents ++= cs }
}
class VertPanel extends BoxPanel(Orientation.Vertical) {
  def glue = { peer.add(jsw.Box.createVerticalGlue); this }
  def strut(n: Int) = { peer.add(jsw.Box.createVerticalStrut(n)); this }
}
object VertPanel {
  def apply(cs: Component*) = new VertPanel { contents ++= cs }
}

当你想添加胶水或支柱时,你只需声明“glue”或“strut(n)”内联:

new VertPanel {
  contents += new Label("Hi")
  glue
  contents += new Label("there")
}

(假设您正在使用该contents +=方法;它实际上并没有为您提供要添加的对象,因此您无法将其与其他项目组合并将它们作为集合添加。)

于 2012-08-05T17:33:14.783 回答