在Java Swing中是否存在一个“组框”?

时间:2021-04-02 07:36:10

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(或者更准确地说,Gupta SQLWindows…)之类的工具在Windows上“画”gui。不知道有多少人知道那是什么;-)。

I can't find an equivalent of a Group Box in Swing...

在Swing中我找不到一个等价的组框……

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).

对于组框,您有一个方形框(通常带有标题),围绕一些相关的小部件。一个例子是围绕几个单选按钮的组框(标题解释单选按钮的含义,例如,组框标题为“性”和“男性”和“女性”单选按钮)。

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?

我搜索了一下……我找到的唯一方法是添加一个子窗格,在子窗格上设置边框,然后将“组”中的所有小部件添加到子窗格中。有更优雅的方法吗?

6 个解决方案

#1


111  

Create a JPanel, and add your radiobuttons to it. Don't forget to set the layout of the JPanel to something appropriate.

创建一个JPanel,并向其中添加radiobutton。不要忘记将JPanel的布局设置为适当的内容。

Then call panel.setBorder(BorderFactory.createTitledBorder(name));

然后调用panel.setBorder(BorderFactory.createTitledBorder(名称));

#2


6  

Others have already commetned about JPanel and using a TitledBorder, that's fine.

其他人已经对JPanel和使用标题边框进行了评论,这没问题。

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 LayoutManagers时,您可能会发现不同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中最好不要使用标题边框,而是在JPanel中使用由JLabel和水平j分隔符组成的独立组件组。

Ref. "First Aid for Swing"

Ref。“Swing急救”

#3


3  

A Group box is just a set of 'logically grouped widgets'. This in the swing world is a JPanel.

组框只是一组“逻辑分组小部件”。在swing中,这是一个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'.

将其边框类型设置为' named border ',并给出标题,与VB6 'frame' frame'的名称相同。

Voila. You have your group box.

瞧。你有你的组盒。

#4


2  

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.)

单选按钮的实现——可以选择或取消选择的项,并向用户显示其状态。与ButtonGroup对象一起创建一组按钮,每次只能选择一个按钮。(创建一个ButtonGroup对象,并使用它的add方法在组中包含JRadioButton对象。)

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对象是一个逻辑分组——而不是物理分组。要创建一个按钮面板,您仍然应该创建一个JPanel或类似的容器对象,并向其添加边框,以将其从周围的组件中设置掉。

#5


1  

Not AFAIK, at least not with standard swing widgets.

不是AFAIK,至少不是标准的swing小部件。

In VB you have a group widget, which is essentially a panel + border.

在VB中有一个group小部件,它本质上是一个面板+边框。

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,它是容器小部件,只有在需要时才在其上创建并设置边框对象。你可以用一种更优雅的方式去争辩,因为你不用花钱买你不用的东西(例如,border)

#6


-1  

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都可以为自己设置边框,所以不需要第二个面板。

#1


111  

Create a JPanel, and add your radiobuttons to it. Don't forget to set the layout of the JPanel to something appropriate.

创建一个JPanel,并向其中添加radiobutton。不要忘记将JPanel的布局设置为适当的内容。

Then call panel.setBorder(BorderFactory.createTitledBorder(name));

然后调用panel.setBorder(BorderFactory.createTitledBorder(名称));

#2


6  

Others have already commetned about JPanel and using a TitledBorder, that's fine.

其他人已经对JPanel和使用标题边框进行了评论,这没问题。

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 LayoutManagers时,您可能会发现不同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中最好不要使用标题边框,而是在JPanel中使用由JLabel和水平j分隔符组成的独立组件组。

Ref. "First Aid for Swing"

Ref。“Swing急救”

#3


3  

A Group box is just a set of 'logically grouped widgets'. This in the swing world is a JPanel.

组框只是一组“逻辑分组小部件”。在swing中,这是一个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'.

将其边框类型设置为' named border ',并给出标题,与VB6 'frame' frame'的名称相同。

Voila. You have your group box.

瞧。你有你的组盒。

#4


2  

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.)

单选按钮的实现——可以选择或取消选择的项,并向用户显示其状态。与ButtonGroup对象一起创建一组按钮,每次只能选择一个按钮。(创建一个ButtonGroup对象,并使用它的add方法在组中包含JRadioButton对象。)

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对象是一个逻辑分组——而不是物理分组。要创建一个按钮面板,您仍然应该创建一个JPanel或类似的容器对象,并向其添加边框,以将其从周围的组件中设置掉。

#5


1  

Not AFAIK, at least not with standard swing widgets.

不是AFAIK,至少不是标准的swing小部件。

In VB you have a group widget, which is essentially a panel + border.

在VB中有一个group小部件,它本质上是一个面板+边框。

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,它是容器小部件,只有在需要时才在其上创建并设置边框对象。你可以用一种更优雅的方式去争辩,因为你不用花钱买你不用的东西(例如,border)

#6


-1  

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都可以为自己设置边框,所以不需要第二个面板。