Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

how to use the layout managers in swing java

I'm new to layout managers like Flow, borders, ...
I mostly use setBounds() to set the position of my components.

I read on an article that using setBounds is not an good practice and it's better to use some layout.

Which are the best and most used layouts?
How to position a button using layout managers; instead of doing setbounds(10,10,100,30)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

look here: http://download.oracle.com/javase/tutorial/uiswing/layout/using.html

Basically you should forget about coordinates. Look at your dialogs at a higher level of design. Questions you should ask yourself.

1) Is there a "main" area with smaller surrounding areas in your design. If so use a BorderLayout.

2) Is there equal grid like areas in your design, If so use a GridLayout.

3) If you need a top-down, or left-right layout, consider a BoxLayout

4) If you want to show a complex form, probably use a FormLayout from jgoodies.

But you have to look at things from a high level. There may be subsections in any one top level section of your dialog. If that is the case, then you need to put a JPanel in that section, and then use a sub layout in that JPanel. Use the above questions over again for that panel.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...