在应用中有一个文本框,当焦点离开此文本框时,作验证,不通过就仍然放置焦点在此文本框中,但是我在focusLost函数中设置setFocus好像无效阿?那位知道是怎么回事?谢谢。
简单代码如下:package com.swt;import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;public class FocusDemo {
protected Shell shell;
private Text txt1;
private Text txt2; public static void main(String[] args) {
try {
FocusDemo window = new FocusDemo();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
} public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
} protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
{
txt1 = new Text(shell, SWT.BORDER);
txt1.setText("Talkweb.com.cn");
txt1.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
}
@Override
public void focusLost(FocusEvent e) {
Text t = (Text)e.getSource();
t.setSelection(0, 5);
t.setFocus();
}
});
txt1.setBounds(10, 39, 97, 36);
}
{
txt2 = new Text(shell, SWT.BORDER);
txt2.setBounds(135, 39, 132, 36);
}
}
}

解决方案 »

  1.   

    代码没问题 ,如果你要把焦点set到一个控件上最好放在相应的函数里XXX.setFocus();
    你添加focus事件只是在他获得焦点后要做的动作
      

  2.   

    当你焦点移到text2上面去的时候,你就失去了text1的焦点了丫,上面选中的,都会消失的。
    你的代码中只有失去焦点的动作,那获得焦点呢?是不是要切换到text1上面去啊
    另:swt没有相应的包不能实际运行帮你改了,见谅
      

  3.   

    谢谢,可是还是不对,如果你能运行以下就好了。我直接在txt2的focusGained函数中加入txt1.setfocus()都不起作用。
      

  4.   

    请看一下代码:public class FocusDemo {

    protected Shell shell;
        private Text txt1;
        private Text txt2;    public static void main(String[] args) {
            try {
                FocusDemo window = new FocusDemo();
                window.open();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    public void open() {
            Display display = Display.getDefault();
            createContents();
            shell.open();
            shell.layout();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        }    protected void createContents() {
            shell = new Shell();
            shell.setSize(450, 300);
            shell.setText("SWT Application");
            {
                txt1 = new Text(shell, SWT.BORDER);
                txt1.setText("Talkweb.com.cn");
                txt1.addFocusListener(new FocusAdapter() {
                    @Override
                    public void focusGained(FocusEvent e) {
                    }
                    @Override
                    public void focusLost(FocusEvent e) {
                        final Text t = (Text)e.getSource();
                        t.getDisplay().asyncExec (new Runnable () {
                            public void run () {
                                t.setSelection(0, 5);
                                t.setFocus();
                            }
                         });                    
                    }
                });
                txt1.setBounds(10, 39, 97, 36);
            }
            {
                txt2 = new Text(shell, SWT.BORDER);
                txt2.setBounds(135, 39, 132, 36);
            }
        }
    }