SWT 文本框Text经过GC重绘改变边框颜色
SWT 文本框Text通过GC重绘改变边框颜色
有需求如下:
在提交数据时,如果SWT文本框数值为空,则改变文本框的边框,以提醒用户.
实现思路:通过绘制GC实现.
代码如下:
Text text = new Text(new Shell(), SWT.NONE); // 在指定情况下重写监听实现.如数据为空时.. if (text.getText().trim().equals("")) { text.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { GC gc = e.gc; gc.setForeground(ImageUtil.txtBorderColor); // 在文本框内实现加一条底线 // gc.drawRectangle(e.x - 1, e.y - 1, e.width + 1, // e.height); // 修改文本框整个边框颜色 gc.drawRectangle(e.x, e.y, e.width - 1, e.height - 1); gc = null; } }); }
-------------------------工作积累 尹当