Text j2me中的换行标签文本

Text j2me中的换行标签文本,text,java-me,label,lwuit,word-wrap,Text,Java Me,Label,Lwuit,Word Wrap,我已经建立了一个列表,并在每个单元格中插入了标签。现在,太长的文本就消失了。我想包装文本,使其在每个单元格内完全可见 你能帮忙吗 更新:问题已解决 对于那些需要答案的人,我在一个容器中使用了LWUIT的HTMLComponent。HTMLComponent允许您使用HTML代码。这将允许您按照您希望的方式设置列表的格式 下面是解决方案的更多细节 在使用LWUIT的JavaME中,我使用了一个HTMLComponent来获得我想要的精确布局。对我来说,最好的方法是在HTMLComponent中使

我已经建立了一个列表,并在每个单元格中插入了标签。现在,太长的文本就消失了。我想包装文本,使其在每个单元格内完全可见

你能帮忙吗

更新:问题已解决 对于那些需要答案的人,我在一个容器中使用了LWUIT的HTMLComponent。HTMLComponent允许您使用HTML代码。这将允许您按照您希望的方式设置列表的格式


下面是解决方案的更多细节

在使用LWUIT的JavaME中,我使用了一个HTMLComponent来获得我想要的精确布局。对我来说,最好的方法是在HTMLComponent中使用HTML表。它的行为就像HTML

String html_code = "";

html_code  = "<table width='100%'>";
html_code += "<tr><td><strong>"+fullname+"</strong></td></tr>";
if (title.length()>0) { html_code += "<tr><td><i>"+title+"</i></td></tr>"; }
if (message.length()>0) { html_code += "<tr><td>"+message+"</td></tr>"; }
if (date.length()>0) { html_code += "<tr><td><i>"+date+"</i></td></tr>"; }
html_code += "</table>";       
             
HTMLComponent html = new HTMLComponent(null);
html.setBodyText(html_code);
String html_code=”“;
html_code=“”;
html_代码+=“”+全名+“”;
如果(title.length()>0){html_code+=“”+title+“”;}
如果(message.length()>0){html_code+=“”+message+“”;}
如果(date.length()>0){html_code+=“”+date+“”;}
html_代码+=“”;
HTMLComponent html=新的HTMLComponent(空);
setBodyText(html_代码);

如果您正在寻找更“优雅”的解决方案,我会在网上找到一个方便的资源。我在这里发帖是为了参考,但是HtmlComponent做这项工作

    import com.sun.lwuit.Font;

/** A class supporting word wrap for MIDP. */

public class WordWrap {

Font font;
int width;
String txt;
int pos;

/**
* Initializes the WordWrap object with the given Font, the text string
* to be wrapped, and the target width.
*
* @param font: The Font to be used to calculate the character widths.
* @param txt: The text string to be wrapped.
* @param width: The line width.
*/

public WordWrap (Font font, String txt, int width) {

this.font = font;
this.txt = txt;
this.width = width;
}

/**
* returns the next line break position. If no text is left, -1 is returned.
*/

public int next () {

int i = pos;
int len = txt.length ();

if (pos >= len) return -1;

int start = pos;

while (true) {
while (i < len && txt.charAt (i) > ' ')
i++;

int w = font.stringWidth (txt.substring (start, i));
if (pos == start) {
if (w > width) {
while (font.stringWidth (txt.substring (start, --i)) > width)
{ }
pos = i;
break;
}
}

if (w <= width) pos = i;

if (w > width || i >= len || txt.charAt(i) == '\n') break;
i++;
}

return pos >= len ? pos : ++pos;
}

}




import com.sun.lwuit.Button;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.FocusListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.Border;
import com.sun.lwuit.plaf.Style;

/**
 *
 * @author rubycube
 */
public class WrapList extends Container {

    private Button hiddenButton;
    private int id;

    public WrapList(String text, int containerID) {
        id = containerID;
        this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        this.setFocusable(false);
        final Style thisContainerStyle = this.getStyle();
        Border thisContainerBorder = Border.createRoundBorder(20, 20, 0xcccccc);
        thisContainerStyle.setBorder(thisContainerBorder);

        hiddenButton = new Button(" ");
        hiddenButton.setPreferredSize(new Dimension(1, 1));
        Style style = hiddenButton.getStyle();
        style.setBgTransparency(0, false);
        style.setBorder(Border.createEmpty());

        FocusListener hiddenButtonFL = new FocusListener() {

            public void focusGained(Component cmp) {

                WrapList parentContainer = ((WrapList) (cmp.getParent()));
                Border parentContainerBorder = Border.createRoundBorder(20, 20, 0xff6600);
                Style parentContainerStyle = parentContainer.getStyle();
                parentContainerStyle.setBorder(parentContainerBorder);
                parentContainerStyle.setBgColor(0xff9900);
                parentContainerStyle.setBgTransparency(50);
                parentContainer.repaint();
            }

            public void focusLost(Component cmp) {
                WrapList parentContainer = ((WrapList) (cmp.getParent()));
                Border parentContainerBorder = Border.createRoundBorder(20, 20, 0xcccccc);
                Style parentContainerStyle = parentContainer.getStyle();
                parentContainerStyle.setBorder(parentContainerBorder);
                parentContainerStyle.setBgTransparency(0);
                parentContainer.repaint();
            }
        };
        hiddenButton.addFocusListener(hiddenButtonFL);

        Label l = new Label(text);
        l.setSelectedStyle(thisContainerStyle);
        //l.setUnselectedStyle(thisContainerStyle);
        WordWrap ww = new WordWrap(l.getStyle().getFont(), text, (Display.getInstance().getDisplayWidth() - 10));
        int si = 0;
        int ei = 0;
        while (true) {
            int np = ww.next();
            if (np == -1) {
                break;
            } else {
                si = ei;
                ei = np;
            }
            String lineText = text.substring(si, ei);
            Label line = new Label(lineText);
            line.setEndsWith3Points(false);
            this.addComponent(line);
        }
        this.addComponent(hiddenButton);
    }

    public void addActionListener(ActionListener actionlistener) {
        hiddenButton.addActionListener(actionlistener);
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }
}
import com.sun.lwuit.Font;
/**支持MIDP换行的类*/
公共类文字包装{
字体;
整数宽度;
字符串txt;
int pos;
/**
*使用给定字体(文本字符串)初始化WordWrap对象
*要进行包装,以及目标宽度。
*
*@param font:用于计算字符宽度的字体。
*@param txt:要包装的文本字符串。
*@param width:线宽。
*/
公共换行字(字体、字符串txt、整型宽度){
this.font=font;
this.txt=txt;
这个。宽度=宽度;
}
/**
*返回下一个换行位置。如果没有剩余文本,则返回-1。
*/
公共整数下一个(){
int i=pos;
int len=txt.length();
如果(pos>=len)返回-1;
int start=pos;
while(true){
而(i“”)
i++;
int w=font.stringWidth(txt.substring(start,i));
如果(位置==开始){
如果(w>宽度){
while(font.stringWidth(txt.substring(start,--i))>width)
{ }
pos=i;
打破
}
}
如果(w width | | i>=len | | txt.charAt(i)='\n')中断;
i++;
}
返回位置>=len?pos:++pos;
}
}
导入com.sun.lwuit.Button;
导入com.sun.lwuit.Component;
导入com.sun.lwuit.Container;
导入com.sun.lwuit.Display;
导入com.sun.lwuit.Label;
导入com.sun.lwuit.events.ActionListener;
导入com.sun.lwuit.events.FocusListener;
导入com.sun.lwuit.geom.Dimension;
导入com.sun.lwuit.layouts.BoxLayout;
导入com.sun.lwuit.plaf.Border;
导入com.sun.lwuit.plaf.Style;
/**
*
*@作者rubycube
*/
公共类WrapList扩展了容器{
私有按钮隐藏按钮;
私有int-id;
公共包装列表(字符串文本,int-containerID){
id=集装箱运输;
这个.setLayout(新的BoxLayout(BoxLayout.Y_轴));
此参数为.setFocusable(false);
最终样式thisContainerStyle=this.getStyle();
Border ThisContainerOrder=Border.createRoundBorder(20,20,0xCCCC);
thisContainerStyle.setBorder(ThisContainerOrder);
hiddenButton=新按钮(“”);
setPreferredSize(新维度(1,1));
Style Style=hiddenButton.getStyle();
风格.立根透明度(0,假);
style.setboorder(Border.createEmpty());
FocusListener hiddenButtonFL=新FocusListener(){
获得公共无效焦点(组件cmp){
WraplistParentContainer=((WrapList)(cmp.getParent());
Border parentContainerOrder=Border.createRoundBorder(20,20,0xff6600);
Style parentContainerStyle=parentContainer.getStyle();
parentContainerStyle.setBorder(ParentContainerOrder);
parentContainerStyle.setBgColor(0xff9900);
父母容器方式。挫折透明度(50);
parentContainer.repaint();
}
公共无效焦点丢失(组件cmp){
WraplistParentContainer=((WrapList)(cmp.getParent());
Border parentContainerOrder=Border.createRoundBorder(20,20,0xCCCC);
Style parentContainerStyle=parentContainer.getStyle();
parentContainerStyle.setBorder(ParentContainerOrder);
parentContainerStyle.SetbGTTransparency(0);
parentContainer.repaint();
}
};
addFocusListener(hiddenButtonFL);
标签l=新标签(文本);
l、 设置选定样式(此容器样式);
//l、 setUnselectedStyle(此容器样式);
WordWrap ww=新的WordWrap(l.getStyle().getFont(),text,(Display.getInstance().getDisplayWidth()-10));
int-si=0;
int ei=0;
while(true){
int np=ww.next();
如果(np==-1){
打破
}否则{
si=ei;
ei=np;
}
字符串lineText=text.substring(si,ei);
标签行=新标签(lineText);
line.SetEndSwith3点(假);
此.addComponent(行);
}
此.addComponent(隐藏按钮);
}
public void addActionListener(ActionListener ActionListener){
addActionListener(actionlistener);
}
/**
*@返回id
*/
公共int getId(){
返回id;
}
/**
*@param id要设置的id
*/
公共无效集合id(内部id){
this.id=id;
}
}

您在使用什么?LCDUI?你穿什么衣服?