问题
Having read over the internet about JPanel
for a while I feel I still need clarification after reading it. My questions:
- What is
JPanel
mostly used for? - Which layout manager is most flexible to use?
- 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:
GridBagLayout
- older, but quite versatile.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.- A third party layout manager like
MigLayout
orFormLayout
回答3:
JPanel
can be used:
- As a container for other components
- Base for a custom widget
- 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:
MigLayout
GroupLayout
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