我正在尝试在SWT中的Text
上绘制边框...
这是我现在得到的:
公共类BorderedText扩展了Text {
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() {
//
}
最佳答案
您有两种可能性: