我想要一个甚至不显示插入符号的不可编辑的 TextField (或子类)。或者,我想要一个多行标签字段。这些有可能吗?
问问题
3776 次
3 回答
8
没有焦点光标的文本字段
TextField readOnly = new TextField(NON_FOCUSABLE);
readOnly.setText("Read only, no carret");
add(readOnly);
TextField drawFocus 覆盖
如果文本太大而无法适应屏幕,您可以覆盖 TextField 中的 drawFocus 方法,这样就可以滚动:
TextField readOnly = new TextField(READONLY)
{
protected void drawFocus(Graphics graphics, boolean on) {}
};
TextFields,用 NullFields 分隔
其他选项是将 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);
于 2009-09-13T10:29:28.270 回答
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 ));
}
}
于 2009-09-13T09:14:53.183 回答
0
你也可以使用RichTextField
带有风格的 a Field.NON_FOCUSABLE
,你就会得到你想要的。
于 2010-02-10T22:46:02.280 回答