Scala Swing组件对齐

时间:2012-11-14 14:28:17

标签: scala scala-swing

我正在学习如何使用Scala上的标准Swing小部件构建GUI。这就是我想要做的事情:

enter image description here

我在创建组件方面取得了成功,但在对齐方面失败了。我的所有组件都对齐居中,不像我想要的那样(左边的Button1,中心的桌子和右边的button2)。我也找不到有关Scala Swing的更多信息。大多数搜索结果都是关于Java的(我对此一无所知)。我该怎么做才能强制对齐?

以下是代码:

ontents += new BoxPanel(Orientation.Vertical) {
    contents += new Button("Button 1")
    contents += new Table(3, 3)
    contents += new Button("Button 2")
}

结果:

enter image description here

先谢谢。

1 个答案:

答案 0 :(得分:4)

我通常使用自己的布局助手(VBox和HBox),这使得大多数东西变得非常简单。但是下面的代码只使用标准库实现了你想要的东西(我也冒昧地在组件之间添加一些边框和间距):

import swing._
import Swing._
import java.awt.Color

new MainFrame {
  val button1 = new Button("Button 1")
  val button2 = new Button("Button 2")
  val table = new Table(4,3) {
    border = LineBorder(Color.BLACK)
  }
  contents = new BoxPanel(Orientation.Vertical) {
    border = EmptyBorder(10)
    contents += new BorderPanel {
      add(button1, BorderPanel.Position.West)
    }
    contents += VStrut(10)
    contents += table
    contents += VStrut(10)
    contents += new BorderPanel {
      add(button2, BorderPanel.Position.East)
    }
  }
  visible = true
}

(您可以将上述内容粘贴到文件中并运行scala file.scala,它会起作用)