Welcome to OGeek Q&A 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...