在j2me中包装标签文本 [英] wrap label text in j2me

查看:71
本文介绍了在j2me中包装标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个列表,并在每个单元格中插入了标签.现在,太长的文本只会消失.我想将文字换行,以便在每个单元格中完全可见.

I've built a list and inserted labels in each cell. For now the text that is too long simply disappear. I'd like to wrap the text so it is entirely visible inside each cell.

可以帮忙吗?

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

For those who need an answer, I used LWUIT's HTMLComponent inside a container. The HTMLComponent allows you to use HTML code. That would allow you to format your list the way you want it to be.

有关此解决方案的更多详细信息.

Here is more details on the solution.

在带有LWUIT的Java ME中,我使用了HTMLComponent来获得所需的精确布局.对我而言,最好的方法是在HTMLComponent中使用HTML表.它的行为就像HTML.

In Java ME with LWUIT, I used a HTMLComponent to get the precise layout I wanted. The best way for me was to use an HTML Table inside the HTMLComponent. It just behaves like 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);

推荐答案

以防万一,如果您正在寻找更优雅"的解决方案,我会在网上找到一个方便的资源.我在这里发布仅供参考,但是HtmlComponent可以完成这项工作.

Just incase if you are looking for a more "elegant" solution, i found a handy resource online. I am posting here for reference purposes, but HtmlComponent does the job.

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

这篇关于在j2me中包装标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆