根据JDialog调整JPanel的大小

时间:2021-03-18 23:16:44

I've got a JDialog which contains a series of JPanels in a CardLayout. Some of these panels contain JTables which I would like to be resized in line with any resizing of the JDialog. I am not sure how to achieve this and any help would be greatly appreciated.

我有一个JDialog,它在CardLayout中包含一系列JPanel。其中一些面板包含JTables,我想根据JDialog的任何大小调整来调整大小。我不确定如何实现这一点,任何帮助将不胜感激。

At present the tables simply remain their current size and do not scale. I'm sure its something simple but I'm having trouble locating the exact approach needed.

目前,表格仅保持其当前大小,不进行缩放。我确信这很简单,但我无法找到所需的确切方法。

Many thanks in advance. I will provide any more information if required.

提前谢谢了。如果需要,我会提供更多信息。

edit: The JDialog is used as a wizard, so only one of the panels is being displayed at any one time, hence the use of CardLayout. I would ideally like to keep this is layout manager, although if it is the source of the problems then obviously I would rethink!

编辑:JDialog用作向导,因此任何时候只显示其中一个面板,因此使用CardLayout。我希望保持这是布局管理器,虽然如果它是问题的根源,那么显然我会重新考虑!

1 个解决方案

#1


1  

You can keep the CardLayout, but you need to do the following: 1. set the layout of your JDialog to BorderLayout, and add a new JPanel (contentPanel) to the JDialog
2. now set this contentPanel layout to be CardLayout
3. add your other panels to the cardlayout as required.
4. Also make sure off the layouts you use on each of the panels you're adding to the CardLayout. By default JPanel uses FlowLayout, I think, and this is not ideal for a JTable. So you might need to play around with the layout of the panel containing the table as well; try out BoxLayout or BorderLayout for that as well.

您可以保留CardLayout,但是您需要执行以下操作:1。将JDialog的布局设置为BorderLayout,并将新的JPanel(contentPanel)添加到JDialog 2.现在将此contentPanel布局设置为CardLayout 3.添加您的卡片布局的其他面板根据需要。 4.还要确保在您添加到CardLayout的每个面板上使用的布局。我认为默认情况下JPanel使用FlowLayout,这对JTable来说并不理想。因此,您可能需要使用包含该表的面板的布局;尝试BoxLayout或BorderLayout也是如此。

#1


1  

You can keep the CardLayout, but you need to do the following: 1. set the layout of your JDialog to BorderLayout, and add a new JPanel (contentPanel) to the JDialog
2. now set this contentPanel layout to be CardLayout
3. add your other panels to the cardlayout as required.
4. Also make sure off the layouts you use on each of the panels you're adding to the CardLayout. By default JPanel uses FlowLayout, I think, and this is not ideal for a JTable. So you might need to play around with the layout of the panel containing the table as well; try out BoxLayout or BorderLayout for that as well.

您可以保留CardLayout,但是您需要执行以下操作:1。将JDialog的布局设置为BorderLayout,并将新的JPanel(contentPanel)添加到JDialog 2.现在将此contentPanel布局设置为CardLayout 3.添加您的卡片布局的其他面板根据需要。 4.还要确保在您添加到CardLayout的每个面板上使用的布局。我认为默认情况下JPanel使用FlowLayout,这对JTable来说并不理想。因此,您可能需要使用包含该表的面板的布局;尝试BoxLayout或BorderLayout也是如此。

相关文章