简体   繁体   中英

How to auto resize java swing elements?

I'm trying to auto resize the left side of my application. I have a JTextField and a JTree on the left and 3 JButtons on the right. But I just don't know how to make the left side auto resizeable.

I did it with the Netbeans GUI Creator (or whatever it is called) but I don't know how to to it without Netbeans. (I usually don't program with Netbeans, this was just an exception to see if it's even possible to do so with Swing.

Here is the code Netbeans created: http://pastebin.com/ERwY4rBC It's not that the code is completely unusable but I wanted to try it manually.

The GroupLayout looks nice, but the Oracle site says it's mainly for the use for GUI tools. So, using GroupLayout would be not "Java like" or how do I have to understand it? Or is there even a better way to achieve this without GroupLayout?

Thanks!

So, using GroupLayout would be not "Java like" or how do I have to understand it

GroupLayout is to put it simply really hard to hand-code, and results mostly in a lot of code. But it is not "not Java like", it is just not something you want to do by hand, and the code afterwards is hard to read as it is rather verbose.

What you try to achieve (according to the screenshot) is easily achievable using some 'nested layouts' . If your main panel uses a BorderLayout where you put the left, resizable panel in the BorderLayout.CENTER and the other, non-resizable panel in the BorderLayout.EAST you will obtain the desired resize behavior.

Then you just have to decide which LayoutManager to use for those individual panels. I think that both the BoxLayout as well as the FlowLayout will do just fine.

Do yourself a favour and use MigLayout for all your layout needs. It is especially convenient for coding UI by hand.

There is a WebStart application on their site that demos different layout situations with code samples provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM