代码之家  ›  专栏  ›  技术社区  ›  noamtm

黑莓Java:TexField*没有插入符号?

  •  1
  • noamtm  · 技术社区  · 15 年前

    我想要一个甚至不显示插入符号的不可编辑文本字段(或子类)。或者,我需要一个多行标签字段。这些有可能吗?

    3 回复  |  直到 15 年前
        1
  •  8
  •   Maksym Gontar    15 年前

    不带焦点光标的文本字段

    TextField readOnly = new TextField(NON_FOCUSABLE);
    readOnly.setText("Read only, no carret");
    add(readOnly);
    

    文本字段DrawFocus覆盖

    如果文本太大,无法适应屏幕,可以在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);
    
        2
  •  3
  •   Henrik P. Hessel    15 年前

    是的,可以重写每个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 ));
        }
    }
    
        3
  •  0
  •   Lucifer phtrivier    13 年前

    您也可以使用 RichTextField 以一种风格 Field.NON_FOCUSABLE 你会得到你想要的。

    推荐文章