简体   繁体   English

在Java Swing中包装文字

[英]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. 我需要使用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. 基本上是为LayoutManager设计的。

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% -------------------->| | <---- 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). 其中一些保留30%规则,但不包装内容,而仅将它们显示在一行中(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. 如果LayoutManager确实支持换行符(即使仅用于HTML文本,但这对我也很好),它将不支持固定宽度。 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. 请注意,我需要坚持使用Swing,并且不能使用其他更高级的库,因为我要开发的系统坚持使用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 . 您可以使用嵌套的BorderLayouts来实现。 Start by setting your Panel's layout as BorderLayout . 首先将面板的布局设置为BorderLayout

After that, for each left and right panels, set layouts as BorderLayout again. 之后,对于每个左面板和右面板,再次将布局设置为BorderLayout At this level, you will set %30 and %70 ratio. 在此级别,您将设置%30和%70比率。

Within this layouts, add your contents to NORTH layouts. 在此布局中,将您的内容添加到NORTH布局。 This will enable your panels' height to match given content. 这将使您的面板高度可以匹配给定的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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