问题描述
我正在尝试在SWT中的 Text
上绘制边框...
这就是我现在得到的:
I'm trying to draw a border over a Text
in SWT...
This is what I got for now:
公共类BorderedText扩展了文本{
public class BorderedText extends 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);
}
}
});
当组件获得焦点时,他就是这样
When the component receive focus, he is like this
但是当我打字时,他被搞砸了
But when I type something, he got screwed up
我想念什么?
编辑
我放弃了 Text
,并使用 StyledText
公共类BorderedText扩展了StyledText {
public class BorderedText extends 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() {
//
}
推荐答案
您有两种可能:
-
您可以使用SWT.BORDER作为样式参数创建一个新的Text小部件.这样会给您一个漂亮的边框.
You can create a new Text widget with SWT.BORDER as a style argument. This would give you a nice looking border.
您可以使用StyledText小部件.StyledText小部件使您可以定义边距.
You can use the StyledText widget. The StyledText widget enables you to define margins.
这篇关于在文本组件上绘制轮廓边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!