3

我正在尝试Text在 SWT 中绘制边框...
这就是我现在得到的:

公共类 BorderedText 扩展文本 {

public BorderedText(Composite parent, int style) {
    super(parent, style);
    addPaintListener(new PaintListener() {

        @Override
        public void paintControl(PaintEvent e) {
            e.gc.setAntialias(SWT.ON);
            if (isFocusControl()) {
                Color color = new Color(getDisplay(), new RGB(82, 168, 236));
                e.gc.setAlpha(200);
                e.gc.setForeground(color);
                Rectangle rect = new Rectangle(0,0, getClientArea().width-1, getClientArea().height-1);

                Transform t = new Transform(getDisplay());
                e.gc.setTransform(t);

                e.gc.drawRoundRectangle(0, 0, rect.width,  rect.height, 4,4);
            } else {
                e.gc.setForeground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
                Rectangle rect = new Rectangle(0,0, getClientArea().width-1, getClientArea().height-1);

                e.gc.drawRectangle(rect);
            }
        }
    });

当组件获得焦点时,他是这样的 在此处输入图像描述

但是当我输入一些东西时,他搞砸了 在此处输入图像描述

我错过了什么?

编辑
我放弃Text并完成了StyledText

公共类 BorderedText 扩展 StyledText {

public BorderedText(Composite parent) {
    super(parent, SWT.WRAP);

    setTabStops(new int[] {0});
    addPaintListener(new PaintListener() {

        @Override
        public void paintControl(PaintEvent e) {
            e.gc.setAntialias(SWT.ON);
            if(isFocusControl()){
                e.gc.setForeground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION));
                e.gc.drawRoundRectangle(0, 0, getClientArea().width-1, getClientArea().height-1, 6,6);
            } else {
                e.gc.setForeground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
                e.gc.drawRoundRectangle(0, 0, getClientArea().width-1, getClientArea().height-1, 6,6);
            }
        }
    });

    addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
            redraw();
        }

        @Override
        public void focusGained(FocusEvent e) {
            redraw();
        }
    });

    addControlListener(new ControlListener() {

        @Override
        public void controlResized(ControlEvent e) {
            redraw();
        }

        @Override
        public void controlMoved(ControlEvent e) {
            redraw();
        }
    });

addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if(e.character == SWT.TAB){
                e.doit = false;
                traverse(SWT.TRAVERSE_TAB_NEXT);
            }
        }
    });
}

@Override
protected void checkSubclass() {
    //
}

@Override
protected void checkWidget() {
    //
}
4

1 回答 1

4

You have two possibilities:

  1. You can create a new Text widget with SWT.BORDER as a style argument. This would give you a nice looking border.

  2. You can use the StyledText widget. The StyledText widget enables you to define margins.

于 2012-07-17T12:57:18.507 回答