在j2me中包装标签文本

时间:2012-07-10 01:24:34

标签: text java-me label lwuit word-wrap

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

你能帮忙吗?

更新:问题已解决

对于那些需要答案的人,我在容器中使用了LWUIT的HTMLComponent。 HTMLComponent允许您使用HTML代码。这样您就可以按照自己想要的方式格式化列表。


以下是有关解决方案的更多详细信息。

在使用LWUIT的Java ME中,我使用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);

1 个答案:

答案 0 :(得分:1)

如果您正在寻找更“优雅”的解决方案,我在网上找到了一个方便的资源。我在这里发帖以供参考,但Html组件可以完成这项工作。

    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;
    }
}