我想要一个甚至没有显示插入符号的不可编辑的TextField(或子类)。或者,我想要一个多行LabelField。这些都有可能吗?
答案 0 :(得分:8)
TextField readOnly = new TextField(NON_FOCUSABLE);
readOnly.setText("Read only, no carret");
add(readOnly);
如果文字太大而无法适应屏幕,您可以在TextField中覆盖drawFocus方法,因此可以滚动:
TextField readOnly = new TextField(READONLY)
{
protected void drawFocus(Graphics graphics, boolean on) {}
};
其他选项是将TextField拆分为多个,用NullFields分隔:
class Scr extends MainScreen {
public Scr() {
String text = "Lorem ipsum dolor sit amet, consectetuer "
+ "adipiscing elit, sed diam nonummy nibh euismod "
+ "tincidunt ut laoreet dolore magna aliquam erat "
+ "volutpat. Ut wisi enim ad minim veniam, quis "
+ "nostrud exerci tation ullamcorper suscipit "
+ "lobortis nisl ut aliquip ex ea commodo consequat. "
+ "Duis autem vel eum iriure dolor in hendrerit in "
+ "vulputate velit esse molestie consequat, vel "
+ "illum dolore eu feugiat nulla facilisis at vero "
+ "eros et accumsan et iusto odio dignissim qui "
+ "blandit praesent luptatum zzril delenit augue "
+ "duis dolore te feugait nulla facilisi.";
text = addScrollText(text, 150);
}
private String addScrollText(String text, int partSize) {
while (0 < text.length()) {
int len = Math.min(partSize, text.length());
TextField readOnly = new TextField(NON_FOCUSABLE);
readOnly.setText(text.substring(0, len));
add(readOnly);
add(new NullField());
text = text.substring(len);
}
return text;
}
}
LabelField中的多行文字,只需使用换行转义字符:
String text = "first line \nnew line \nanother line";
LabelField multiLine = new LabelField(text);
add(multiLine);
答案 1 :(得分:3)
是的,您可以覆盖每个UI元素的绘制方法
一个例子:
public class WhiteLabelField extends LabelField
{
public WhiteLabelField()
{
super();
}
public WhiteLabelField(ObjectGroup text)
{
super(text);
}
public WhiteLabelField(Object text, long style)
{
super(text, style);
}
public void paint(Graphics _g)
{
_g.setColor(Color.WHITE);
super.paint(_g);
}
// Custom
public void setSmallFontSize()
{
setFont( Font.getDefault( ).derive( Font.PLAIN, 16 ));
}
}
答案 2 :(得分:0)
你也可以使用风格为RichTextField
的{{1}},你会得到你想要的。