将 WrapLayout 与多个相邻的 JPanel 一起使用会导致面板大小问题 [英] Using WrapLayout with multiple adjacent JPanels is causing Panel sizing issues

查看:38
本文介绍了将 WrapLayout 与多个相邻的 JPanel 一起使用会导致面板大小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个相邻的 JPanel,每个都使用 WrapLayout 来显示可变数量的 1x16 网格.

它适用于包含最多 1x16 网格的面板,但是,其他两个面板的尺寸似乎由包含最多 1x16 网格的面板决定.例如,如果面板 1 和面板 2 分别只有一个 1x16 网格,但面板 3 有十个 1x16 网格分布在两行中,那么面板 1 和面板 2 在它们各自的 1x16 网格下方都有一个 1x16 网格大小的空白空间.

我基本上希望调整每个面板的大小以独立于其他面板运行.我已经查看了 camickr 的 WrapLayout src 代码,但无法弄清楚我应该更改什么才能获得所需的结果.

在代码方面,很难知道如何包含一个简洁而有指导意义的部分,因为代码非常分散,我不确定是哪个部分导致了这个问题.我只是将面板的布局设置为左对齐的 WrapLayout.

我在下面添加了一张图片,可能会提供一些背景信息.该图像显示了三个 JPanel 中的两个(青色)的一部分,以及其中一个 JPanel(绿色)的随附标题 JLabel.每个面板上的 1x16 网格使用 camickr 的 WrapLayout 进行管理.所有三个青色 JPanel 都包含在一个更大的显示 JPanel 中,然后该 JPanel 已打包到 JScrollPane(因此图像右侧的灰色滚动条).

任何帮助将不胜感激.

这是我创建的另一张图片,它更清楚地显示了这个问题.图像中的第三个面板换行到一个新行以显示它包含的所有 1x16 网格,这样做会使其他两个面板的高度加倍,尽管它们不需要调整大小.

这是我希望 GUI 显示的方式:

解决方案

从期望的行为和实际的行为图像来看,问题在于你用来将 3 个面板布局成的 LayoutManager他们的父面板/框架(我猜是 GridLayout 因为这 3 个面板的大小分布均匀).如果是这样,请使用其他一些 LayoutManager,例如垂直 BoxLayout.

示例代码和教程可以在Oracle对应的(其中的实际代码示例,可以在 this 带有花哨代码着色的链接,或 此处 为纯文本).

I have 3 adjacent JPanels that each use WrapLayout to display a variable number of 1x16 grids.

It works great for the panel which contains the most 1x16 grids, however, the other two panels' dimensions seem to be determined by the panel that contains the most 1x16 grids. If, for example, panels 1 and 2 have only one 1x16 grid respectively, but panel 3 has ten 1x16 grids spread across two rows, then panel 1 and 2 both have a 1x16 grid-sized empty space beneath their respective 1x16 grids.

I basically want to get each panel's resizing to operate independently of the other panels. I've looked through camickr's src code for WrapLayout and can't quite work out what I should change to get the desired result.

In terms of code, it's tricky to know how to include a succinct and instructive section, as the code is quite spread out and I'm not sure what part is causing this issue. I'm just setting the panels' layout as a left-justified WrapLayout.

I've included an image below which might provide some context. The image shows part of two of the three JPanels (coloured cyan) and the accompanying title JLabel for one of those JPanels (coloured green). The 1x16 grids on each panel are managed using camickr's WrapLayout. All three cyan JPanels are contained within a larger display JPanel that has then been packed into a JScrollPane (hence the grey scroller to the right of the image).

Any help would be really appreciated.

Here's another image I created that shows the issue a little more clearly. The third panel in the image wraps to a new row to display all the 1x16 grids that it contains, and in so doing, doubles the height of the other two panels, despite them not needing to resize.

Here's how I want the GUI to display:

解决方案

Judging from the desired behaviour vs the actual behaviour images, the problem lies in the LayoutManager you use to lay out the 3 panels into their parent panel/frame (which I am guessing is a GridLayout because the 3 panels have equally distributed sizes). If so, use some other LayoutManager, such as a vertical BoxLayout.

An example code and tutorial can be found in the Oracle's corresponding one (the actual code sample of which, can be found in this link with fancy code coloring, or here as plain text).

这篇关于将 WrapLayout 与多个相邻的 JPanel 一起使用会导致面板大小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆