我正在尝试实现一种方法,如果用户没有输入任何内容,就像文本一样在Control周围绘制红色边框。我正在使用eclipse swt。 我的方法看起来像这样:
protected void drawRedBorder(Control cont){
final Control control = cont;
cont.getParent().addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e){
GC gc = e.gc;
Color red = new Color(null, 255, 0 ,0);
gc.setBackground(red);
Rectangle rect = control.getBounds();
Rectangle rect1 = new Rectangle(rect.x - 2, rect.y - 2,
rect.width + 4, rect.height + 4);
gc.setLineStyle(SWT.LINE_SOLID);
gc.fillRectangle(rect1);
}
});
}
当我在创建带Textfield的对话框时调用它时,它工作正常。但是,当我在像checkInput()这样的方法中调用它时,它会检查用户是否输入了某些东西。
我尝试通过调用redraw()或update()来解决问题,但没有任何效果。我有什么方法可以解决这个问题吗?
答案 0 :(得分:0)
尝试以下方法。
import org.eclipse.swt.*;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
public class Demo {
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
final Text txt;
txt= new Text(shell, SWT.BORDER);
drawRedBorder(txt);
txt.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent arg0) {
// TODO Auto-generated method stub
txt.getParent().redraw();
}
});
shell.setLayout(new GridLayout());
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
protected static void drawRedBorder(Control cont){
final Control control = cont;
cont.getParent().addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e){
if(((Text) control).getText().length()<=0){
GC gc = e.gc;
Color red = new Color(null, 255, 0 ,0);
gc.setBackground(red);
Rectangle rect = control.getBounds();
Rectangle rect1 = new Rectangle(rect.x - 2, rect.y - 2,
rect.width + 4, rect.height + 4);
gc.setLineStyle(SWT.LINE_SOLID);
gc.fillRectangle(rect1);
}
}
});
}
}