Align Swing Components across Panels

…衆ロ難τιáo~ 提交于 2019-12-19 07:26:05

问题


We have a JPanel wich contains multiple JPanels wich contain JComponents (let's say JLabels and JTextboxes)

Inside each of the internal JPanels we use JGoodies Layout in order to ensure proper alignement of all Labels.

But of course we would like to have all the Labels aligned independently on which subpanel they are.

How could we do that, without fixing the width of the column which contains the JLabels?

We can't loose the JPanels since we have to have Borders around groups of Components


回答1:


Just because the JPanels have borders, doesn't mean that they actually need to contain their apparent contents. Set the panels to transparent. Add the panels and the components to the enclosing panel. Add spacer components to mimic the insets of the panels in the layout. You will also need to switch off "optimised drawing", or some such, for overlapping components.




回答2:


I recommend to favor flat layouts over nested ones. In a single layout alignment is easy. Avoid TitledBorders and replace them with titled separators, separators, or just white space. That helps for the vast majority of editors and forms.

But if you want to align across multiple editors or forms, the above technique fails. The JGoodies FormLayout provides two levels to address this problems, and more generally to improve layout consistency: 1) lower bounds for sizes, 2) layout variables.

With 1) you can describe layouts that ensure a minimum width across forms. For example, if you want to say that all label columns have at least a width of 100px, you can say "[100px, pref]" for the label column.

2) goes beyond approach 1). And the motivation is to extract the 100px from your many forms. In FormLayout you can setup layout variables, for example $label that you configure as "[100px, pref]" or "right:[75dlu, pref]", etc. If you use the layout variable in all your editors, these will be consistent and you have a single place where you can configure all label columns for all editors.




回答3:


There's no simple way to do that which I'm aware of. Your options:

  • Write your own layout manager (or extend an existing one) with that capability
  • Fixed column widths
  • Decide that panels that are visually separated by borders don't need to have their contents aligned after all



回答4:


I've read suggestions about using JSeparator rather than multiple JPanels with borders, if aligning widgets is your priority.

JSeparator gives the visual effect of grouping widgets, but themselves are just another simple widget fitting into the same JPanel. Read the tutorial for important tips such as setting the preferred size. http://download.oracle.com/javase/tutorial/uiswing/components/separator.html

Also, Apple's current guidelines suggest simply using empty space as a divider between groupings, as an alternative to borders and separators.




回答5:


This sample code can help to fix your alignment problem:

//c is instance of the content pane
c.setLayout(gl=new GridLayout(3,0));
//jp is a jpanel. I took this panel because there is no alignment
//setting for a specifiq cell of grid Layout.
jp.setAlignmentX(CENTER_ALIGNMENT);
//jl is the jLabel
jp.add(jl);
//finally I add that with the frame
c.add(jp);

Hope it will help you to solve your issue.



来源:https://stackoverflow.com/questions/3761270/align-swing-components-across-panels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!