JPanel and Java layout managers

那年仲夏 提交于 2019-12-25 08:16:53

问题


Having read over the internet about JPanel for a while I feel I still need clarification after reading it. My questions:

  1. What is JPanel mostly used for?
  2. Which layout manager is most flexible to use?
  3. How do you position components at certain areas using layout managers?

I find it really difficult to position components at certain areas within the container using a layout manager.

What layout manager do you recommend I use or do you have to use a mixture of different ones?


回答1:


My first question is what is JPanel mostly used for.

You already know that. You add components to the panel.

My second question is which layout manager is most flexible to use

The more flexible that layout manager, the more complicated the layout manager is to use. So the trick is design your GUI logically and then use multiple panels with different layout manager to achieve your desire effect.

Each layout manager has its own strengths and weaknesses.

I find it really difficult to position components at certain areas within the container using a layout manager.

That is old thinking. You should not be trying to position components specifically. You position components generally and let the layout manager adjust as the size of the frame is changed by the user.

Maybe you want components centered, or in a grid. There are different layout managers to achieve your task. We can't give specific advice. If you have a specific problem then as a specific question and post your code that shows what you have tried.

But first, read the Swing tutorial on Layout Manager and download the example code play with the code to understand how each works. You learn by trying.




回答2:


What layout manager do you recommend I use or do you have to use a mixture of different ones.

It depends on the GUI. I'd typically use a combination of layout managers for anything but the most trivial user interfaces. On the other hand, some like to try and use a more complex layout for those same GUIs, using layouts like:

  1. GridBagLayout - older, but quite versatile.
  2. GroupLayout - newer, more powerful, and provides better ability to align elements that are not next to each other, but considered so difficult to hand write that most use a GUI designer that will produce code that will not be portable to the next GUI designer or IDE.
  3. A third party layout manager like MigLayout or FormLayout



回答3:


JPanel can be used:

  1. As a container for other components
  2. Base for a custom widget
  3. Drawing area

MigLayout is hands down the most flexible layout manager.

There are many layout managers in Swing, but in my opinion only three are up to the job:

  1. MigLayout
  2. GroupLayout
  3. FormLayout

Since MigLayout is an (greatly) improved FormLayout, I usually recommend the first two managers.

Using a mixture of various managers is a poor practice. It is a way of overcoming the simplicity of basic layout managers. One should choose a good layout manager and not to tangle with multiple layout managers. Most layouts can be easily done with MigLayout and GroupLayout.



来源:https://stackoverflow.com/questions/40660365/jpanel-and-java-layout-managers

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