在应用中有一个文本框,当焦点离开此文本框时,作验证,不通过就仍然放置焦点在此文本框中,但是我在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);
}
}
}
简单代码如下: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);
}
}
}
解决方案 »
- java初学者提问,谢谢解答
- 求救:改了一个用SMTP发邮件的Java代码,但是总是530 SMTP authentication Error ,开发工具Netbean
- 请教JDBC连接 Sqlserver 非默认的实例名问题!
- 关于网站安全性问题,前辈请进
- 如何在Applet内用专用数据库驱动程序访问ACCESS数据库呀????
- 如何显示在sqlserver中存放在image类型字段中的bmp格式的图片
- 谁能将1122010103转成2005-7-22 13:28?
- 用MS1.1的JVM运行的APPLET是不是不能连接SQL-SERVER数据库?(急啊,发了一贴没人回啊
- 在applet中如何弹出对话框
- JBuilder 5 的注册码?
- 关于java的一个问题
- 怎么写java向手机发送短信
你添加focus事件只是在他获得焦点后要做的动作
你的代码中只有失去焦点的动作,那获得焦点呢?是不是要切换到text1上面去啊
另:swt没有相应的包不能实际运行帮你改了,见谅
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);
}
}
}