如何使用GridLayout设置组件大小?有没有更好的办法? [英] How to set the component size with GridLayout? Is there a better way?

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

问题描述

我正在使用Java开发更大的GUI,我对布局管理器感到愤怒。

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

我有一个设置 - 面板,其中包含可变数量的JComponents在它(标签,按钮,JSpinners,JSliders,...)。我只想要以下内容:

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

...

我的面板大小为500px,因此有足够的空间容纳很多组件。不幸的是,即使我为每个组件设置了MaximumSize,GridLayout总是会将组件的大小拉伸到整个Panel。如果只有两个按钮,每个高度为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,但是Component总是居中,而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设置组件大小?有没有更好的办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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