简体   繁体   中英

Wrapping Text in Java Swing

Okay, I am kind of desperate right now. I hope you guys can help.

I need to layout content panels with Java Swing. The Problem is, that every content is different. So I need a panel that resize itself for every content. Basically what LayoutManagers are invented for.

I need a left panel and a right panel. The widths of the panels should be fixed. The heights should adjust to the given content

|<---- 30% ------->|<----- 70% -------------------->|

Easy going I thought, but it just wont work. I tried different layout managers. Some of them keep the 30% rule, but doesn't wrap the content and just display them in one single line (BorderLayout). If a LayoutManager does support line-break (even if its just for HTML text but that is fine for me) it wont support the fixed width. A combination of both didn't worked for me either.

Note that I need to stick to Swing and can not use another more advanced library because the system I am developing for is stuck to Java 1.5. Furthermore, I know the total screenwidth so I could calculate the width of the panels to work with fixed widths, but I need to be flexible with the height.

You can achieve this by using nested BorderLayouts . Start by setting your Panel's layout as BorderLayout .

After that, for each left and right panels, set layouts as BorderLayout again. At this level, you will set %30 and %70 ratio.

Within this layouts, add your contents to NORTH layouts. This will enable your panels' height to match given content.

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