将 ButtonGroup 添加到 Box-layout 对象

发布于 2024-12-19 08:16:20 字数 191 浏览 2 评论 0原文

当我尝试将 ButtonGroup 对象放入我的 Box 对象时,编译器返回以下错误:

没有这种类型的方法

请帮助我,如何将我的 ButtonGroup 添加到 Horizo​​ntal Box 中?

When I try to put ButtonGroup object to my Box object, compiler returns the following error:

no method for such a type

Please help me, how can I add my ButtonGroup into Horizontal Box?

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

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

发布评论

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

评论(2

带上头具痛哭 2024-12-26 08:16:20

像这样的事情:

ButtonGroup bg; // your button group
Box box; // your box
// Create a panel to group the buttons.
JPanel panel = new JPanel();
// Add all of the buttons in the group to the panel.
for (Enumeration<AbstractButton> en = buttonGroup.getElements(); en.hasMoreElements();) {
    AbstractButton b = en.nextElement();
    panel.add(b);
}
// Add the panel to the box.
box.add(panel):

Something like this:

ButtonGroup bg; // your button group
Box box; // your box
// Create a panel to group the buttons.
JPanel panel = new JPanel();
// Add all of the buttons in the group to the panel.
for (Enumeration<AbstractButton> en = buttonGroup.getElements(); en.hasMoreElements();) {
    AbstractButton b = en.nextElement();
    panel.add(b);
}
// Add the panel to the box.
box.add(panel):
心凉怎暖 2024-12-26 08:16:20

ButtonGroup 扩展了 Object;它不是一个组件。因此它没有显式添加到容器或组件中。相反,它对 AbstractButton 实例进行分组。

以下是 Java 文档中的示例代码

不将 ButtonGroup 设为组件的一个优点(可能也是以这种方式实现它的原因)是,您可以让不同组件上的 AbstractButton 实例成为同一 ButtonGroup 的成员。
下面是一些使用 BoxLayout 来演示它的示例代码。

JPanel mainPanel = new JPanel();
mainPanel.setLayout ( new BoxLayout( mainPanel, BoxLayout.PAGE_AXIS ) );

ButtonGroup group = new ButtonGroup( );

JButton dogButton = new JButton("dog");
group.add( dogButton );
JPanel dogPanel = new JPanel( );
dogPanel.add( dogButton );
mainPanel.add( dogPanel );

JButton catButton = new JButton("cat");
group.add( catButton );
JPanel catPanel = new JPanel();
catPanel.add( catButton );
mainPanel.add( catPanel );

The ButtonGroup extends Object; it is not a Component. So it is not explicitly added to a Container or Component. Rather, it groups AbstractButton instances.

Here is the example code from the Java documentation.

One advantage of not making ButtonGroup a Component (and probably the reason for implementing it this way) is that you can have AbstractButton instances on different Components be member of the same ButtonGroup.
Here is some sample code to demonstrate it, using a BoxLayout.

JPanel mainPanel = new JPanel();
mainPanel.setLayout ( new BoxLayout( mainPanel, BoxLayout.PAGE_AXIS ) );

ButtonGroup group = new ButtonGroup( );

JButton dogButton = new JButton("dog");
group.add( dogButton );
JPanel dogPanel = new JPanel( );
dogPanel.add( dogButton );
mainPanel.add( dogPanel );

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