简体   繁体   English

Swing GUI组件太紧张了

[英]Swing GUI components too tighly packed

In the Java Swing app I made it seems to me that all the component are too tightly packed. 在Java Swing应用程序中,我认为所有组件都太紧密了。

In QT one can specify padding and margins for the layout. 在QT中,可以指定布局的填充和边距。

Is there something similar for swing? 摇摆有类似的东西吗?

alt text http://img12.yfrog.com/img12/9612/screenshotscreenerconfi.png 替代文字http://img12.yfrog.com/img12/9612/screenshotscreenerconfi.png

Here is a screen shot of my application that I thing is too tight (is it? what do you think?. 这是我的应用程序的屏幕截图,我的事情太紧了(是吗?你怎么看?)

Thanks. 谢谢。

You could use MiGLayout as your layout manager. 您可以使用MiGLayout作为布局管理器。 It allows all kinds of customizations, including margins/paddings. 它允许各种自定义,包括边距/填充。

Take a look to the GridBagLayoutManager . 看一下GridBagLayoutManager Its the most compex layout manager but everything can be acomplished whith it. 它是最复杂的布局管理器,但一切都可以完成。

It uses the GridBagConstraintObject which has the inset property, it specifies the separation to the top, bottom, left and right components. 它使用具有inset属性的GridBagConstraintObject ,它指定与顶部,底部,左侧和右侧组件的分离。

example: GridBagConstraintObject.insets.left=20 示例:GridBagConstraintObject.insets.left = 20

You could achieve a much better layout for the example above by using DesignGridLayout in just a couple of lines of code (one per row in your layout). 通过在几行代码中使用DesignGridLayout (布局中每行一个),您可以为上面的示例实现更好的布局。 DesignGridLayout will automatically use the correct spacing for the runtime platform. DesignGridLayout将自动为运行时平台使用正确的间距。

besides I would highly suggest that you DON'T use TitledBorders in your form because it prevents ANY LayoutManager (as advanced as it may be) from automatically aligning correctly the various components across different groups. 此外,我强烈建议您不要在表单中使用TitledBorders,因为它可以防止任何LayoutManager(尽可能先进)自动正确地对齐不同组中的各种组件。 Instead you could use a JLabel with a JSeparator (there are examples in DesignGridLayout, but this works with any other LayoutManager). 相反,您可以将JLabel与JSeparator一起使用(DesignGridLayout中有一些示例,但这适用于任何其他LayoutManager)。

Since Java 1.6 swing there is a new GroupLayout manager that make this kind of works easier. 从Java 1.6开始,有一个新的GroupLayout管理器,使这种工作更容易。

For instance there is a method: setAutoCreateGaps() that: 例如,有一个方法: setAutoCreateGaps()

...you add two components to a SequentialGroup a gap between the two components is automatically be created... ...您将两个组件添加到SequentialGroup中,将自动创建两个组件之间的间隙...

For instance: 例如:

替代文字

What LayoutManager are you using? 你在使用什么LayoutManager Adding margins is quite easy, it depends however on the specific LayoutManager used. 添加边距非常简单,但它取决于所使用的特定LayoutManager。

FormLayout is another good layout manager. FormLayout是另一个优秀的布局管理器。 With a good GUI editor like JFormDesigner it makes GUI building easy enough. 使用像JFormDesigner这样的优秀GUI编辑器,它使GUI构建变得足够简单。 JFormDesigner actually automatically adds sufficient padding in most cases. 在大多数情况下,JFormDesigner实际上会自动添加足够的填充。 I have to recommend against using GridBagLayout. 我不得不建议不要使用GridBagLayout。 It does the job alright, but is very complex which makes it difficult to use and maintain. 它的工作正常,但非常复杂,难以使用和维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM