如何在scala中创建一个包含RadioButtons的新ButtonGroup?

发布于 2024-12-05 15:31:31 字数 399 浏览 5 评论 0原文

我在使用 Scala 编程语言创建包含单选按钮的 ButtonGroup 时遇到问题。我使用的代码如下:

val buttongroup = new ButtonGroup {
  buttons += new RadioButton("One")
  buttons += new RadioButton("Two")
}

我的用于显示按钮组的代码位于 BorderPanel 内:

layout += new BoxPanel(Orientation.Vertical) {
  buttongroup
} -> BorderPanel.Position.West

但是,没有显示任何内容...我已经查阅了 API,但不确定出了什么问题!

I am having trouble creating a ButtonGroup containing radio buttons in the Scala Programming Language. The code I am using is as following:

val buttongroup = new ButtonGroup {
  buttons += new RadioButton("One")
  buttons += new RadioButton("Two")
}

and my code for displaying the button group is within a BorderPanel:

layout += new BoxPanel(Orientation.Vertical) {
  buttongroup
} -> BorderPanel.Position.West

However, nothing displays... I've consulted the API and I'm not sure what is wrong!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

胡大本事 2024-12-12 15:31:32

您应该将包含按钮的列表添加到面板,而不是按钮组本身,例如:


val radios = List(new RadioButton("One"), new RadioButton("two"))
layout += new BoxPanel(Orientation.Vertical) {
  contents ++= radios         
}

另请参阅 此示例位于 scala swing 包本身中。

You should add a list containing the buttons to the panel, not the buttongroup itself, e.g.:


val radios = List(new RadioButton("One"), new RadioButton("two"))
layout += new BoxPanel(Orientation.Vertical) {
  contents ++= radios         
}

See also this example in the scala swing package itself.

杯别 2024-12-12 15:31:32

虽然按钮组使按钮互斥,但您仍然需要向面板添加单独的按钮。您可以使用 ButtonGroup.buttons 获取按钮列表:

layout += new BoxPanel(Orientation.Vertical) {
  val buttongroup = new ButtonGroup {
    buttons += new RadioButton("One")
    buttons += new RadioButton("Two")
  }
  contents ++= buttongroup.buttons
} -> BorderPanel.Position.West

如果您希望在创建工具栏时选择第一个按钮,您可以添加:

buttongroup.select(buttongroup.buttons .head)

While the button group makes the buttons mututaly exclusive, you still need to add individual buttons to the panel. You can use ButtonGroup.buttons to obtain the list of the buttons:

layout += new BoxPanel(Orientation.Vertical) {
  val buttongroup = new ButtonGroup {
    buttons += new RadioButton("One")
    buttons += new RadioButton("Two")
  }
  contents ++= buttongroup.buttons
} -> BorderPanel.Position.West

If you want the first button to be selected when the toolbar is created, you can add:

buttongroup.select(buttongroup.buttons.head)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文