本文介绍了如何使用 GridLayout 设置组件大小?有没有更好的办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java 开发更大的 GUI,但我对布局管理器很生气.

I am working on a larger GUI with Java and I am becoming angry on Layout Managers.

我有一个设置面板",其中包含可变数量的 JComponent(标签、按钮、JSpinner、JSlider 等).我只想要以下内容:

I have a "Settings-Panel" with a variable number of JComponents in it (Labels, Buttons, JSpinners, JSliders,...). I just want the following:

JLabel JComponent

JLabel <-> JComponent

JLabel JComponent

JLabel <-> JComponent

JLabel JComponent

JLabel <-> JComponent

...

我的面板大小为 500 像素,因此有足够的空间容纳很多组件.不幸的是 GridLayout 总是将组件的大小拉伸到整个面板,即使我为每个组件设置了一个 MaximumSize.如果只有两个高度为 250px 的按钮看起来很愚蠢.

My Panel has a size of 500px, so that there is enough space for a lot of components. Unfortunatly the GridLayout always stretches the size of the Components to the whole Panel, even if I set a MaximumSize for every component. It looks stupid if there are only two buttons each with a height of 250px.

我尝试了流布局,但我无法找到正确创建新行的方法.我试过BoxLayout.Y_AXIS,但是Components总是居中,Label和Component不在同一行.

I tried flow Layout, but i cannot figure out a way to make new lines properly. I tried BoxLayout.Y_AXIS, but the Components are always centered, and Label and Component are not in the same line.

有没有人知道使用 LayoutManagers 正确处理这个问题的好方法?

Does anybody know a good and short way with LayoutManagers to handle this properly?

推荐答案

其他布局的替代方案,可能是将带有 GridLayout 的面板放在另一个 FlowLayout 面板中.这样,您的间距将保持不变,但不会扩展到整个可用空间.

An alternative to other layouts, might be to put your panel with the GridLayout, inside another panel that is a FlowLayout. That way your spacing will be intact but will not expand across the entire available space.

这篇关于如何使用 GridLayout 设置组件大小?有没有更好的办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 22:35