Java Swing 中是否有等效的“Group Box”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/431654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a "Group Box" equivalent in Java Swing?
提问by Thorsten
Trying to build a GUI application in Java/Swing. I'm mainly used to "painting" GUIs on the Windows side with tools like VB (or to be more precise, Gupta SQLWindows... wonder how many people know what that is ;-)).
尝试在 Java/Swing 中构建 GUI 应用程序。我主要习惯于使用像 VB 这样的工具在 Windows 端“绘制”GUI(或者更准确地说,Gupta SQLWindows ......想知道有多少人知道那是什么;-))。
I can't find an equivalent of a Group Box in Swing...
我在 Swing 中找不到相当于 Group Box 的东西...
With a group box, you have a square box (usually with a title) around a couple of related widgets. One example is a group box around a few radio buttons (with the title explaining what the radio buttons are about, e.g. Group Box entitled "Sex" with "Male" and "Female" radio buttons).
使用分组框,您有一个围绕几个相关小部件的方形框(通常带有标题)。一个例子是围绕几个单选按钮的分组框(标题解释了单选按钮的含义,例如标题为“Sex”的分组框带有“男性”和“女性”单选按钮)。
I've searched around a bit... the only way I found was to add a sub-pane, set the border on the sub-pane and then add all the widgets in the "group" to the sub-pane. Is there a more elegant way to do that?
我已经搜索了一下......我发现的唯一方法是添加一个子窗格,在子窗格上设置边框,然后将“组”中的所有小部件添加到子窗格中。有没有更优雅的方法来做到这一点?
采纳答案by David Koelle
Create a JPanel, and add your radiobuttons to it. Don't forget to set the layout of the JPanel to something appropriate.
创建一个 JPanel,并将您的单选按钮添加到其中。不要忘记将 JPanel 的布局设置为适当的内容。
Then call panel.setBorder(BorderFactory.createTitledBorder(name));
然后打电话 panel.setBorder(BorderFactory.createTitledBorder(name));
回答by Uri
Not AFAIK, at least not with standard swing widgets.
不是 AFAIK,至少不是标准的摆动小部件。
In VB you have a group widget, which is essentially a panel + border.
在 VB 中,您有一个组小部件,它本质上是一个面板 + 边框。
In Swing you have a JPanel which is the container widget, and you create and set a border object on it only if you need one. One can argue that in a way that is more elegant since you don't pay for something you don't use (e.g., border)
在 Swing 中,您有一个 JPanel,它是容器小部件,只有在需要时才在其上创建和设置边框对象。人们可以以一种更优雅的方式争辩说,因为您不会为不使用的东西付费(例如,边框)
回答by Geo
I'm responding based on the Uri's comment which explaind what the OP meant by Group Box
:
我根据 Uri 的评论做出回应,该评论解释了 OP 的含义Group Box
:
Uri: I think he means the control group you see in many dialog boxes, where you have a square around a bunch of widgets such as radio buttons, for example.
Uri:我认为他的意思是您在许多对话框中看到的控制组,例如,在一堆小部件(例如单选按钮)周围有一个正方形。
As far as I know, every JComponent
can set a border for itself, so you don't need a second panel.
据我所知,每个人JComponent
都可以为自己设置一个边框,所以你不需要第二个面板。
回答by Nivas
A Group box is just a set of 'logically grouped widgets'.
This in the swing world is a JPanel
.
组框只是一组“逻辑分组的小部件”。这在秋千世界中是一个JPanel
.
Add your widgets to a JPanel
.
将您的小部件添加到JPanel
.
Set its border type to 'Titled Border' and give the title, same as the name of the VB6 'frame'.
将其边框类型设置为'Titled Border'并给出标题,与VB6'frame'的名称相同。
Voila. You have your group box.
瞧。你有你的分组框。
回答by jfpoilpret
Others have already commetned about JPanel and using a TitledBorder, that's fine.
其他人已经对 JPanel 进行了讨论并使用了 TitledBorder,这很好。
However, when playing with Swing LayoutManagers, you may find it annoying that components in different JPanels cannot align correctly (each panel has its own LayoutManager).
但是,在使用 Swing LayoutManager 时,您可能会发现不同 JPanel 中的组件无法正确对齐(每个面板都有自己的 LayoutManager)很烦人。
For this reason, it is a good practice (check "JGoodies" on the web for more details) in Swing GUIs to NOT use TitledBorders but rather separate groups of components in a JPanel by a JLabel followed by a horizontal JSeparator.
出于这个原因,在 Swing GUI 中不使用 TitledBorders 而是通过 JLabel 后跟水平 JSeparator 分隔 JPanel 中的组件组是一个很好的做法(查看网络上的“JGoodies”了解更多详细信息)。
Ref. "First Aid for Swing"
参考 《秋千急救》
回答by Bill
Here's a quote from the JRadioButton javadocs since you brought up radio buttons.
这是 JRadioButton javadocs 中的引用,因为您提出了单选按钮。
An implementation of a radio button -- an item that can be selected or deselected, and which displays its state to the user. Used with a ButtonGroup object to create a group of buttons in which only one button at a time can be selected. (Create a ButtonGroup object and use its add method to include the JRadioButton objects in the group.)
Note: The ButtonGroup object is a logical grouping -- not a physical grouping. To create a button panel, you should still create a JPanel or similar container-object and add a Border to it to set it off from surrounding components.
单选按钮的实现——可以选择或取消选择的项目,并向用户显示其状态。与 ButtonGroup 对象一起使用以创建一组按钮,其中一次只能选择一个按钮。(创建一个 ButtonGroup 对象并使用其 add 方法将 JRadioButton 对象包含在组中。)
注意:ButtonGroup 对象是一个逻辑分组——而不是一个物理分组。要创建按钮面板,您仍然应该创建一个 JPanel 或类似的容器对象,并向其添加一个边框以将其与周围的组件分开。