请教SWT中Text的setFocus方法无效问题?谢谢.

luoxiang2000 2009-07-07 10:59:23
在应用中有一个文本框,当焦点离开此文本框时,作验证,不通过就仍然放置焦点在此文本框中,但是我在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);
}
}
}
...全文
516 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zuijiejina 2009-07-07
  • 打赏
  • 举报
回复
当你焦点移到text2上面去的时候,你就失去了text1的焦点了丫,上面选中的,都会消失的。
你的代码中只有失去焦点的动作,那获得焦点呢?是不是要切换到text1上面去啊
另:swt没有相应的包。。。不能实际运行帮你改了,见谅
luoxiang2000 2009-07-07
  • 打赏
  • 举报
回复
代码没有问题吗?您是否运行了一下?我这里运行以后,好像没有任何作用。
此代码有两个文本框,首先焦点在第一个文本框里面,然后我鼠标点入第二个文本框,本来我代码的意图是让第一个文本框中
的1-5个字符被选中,然后焦点仍然落在第一个文本框中。可是运行结果这个意图根本就没有实现???????
谢谢。
njclsc 2009-07-07
  • 打赏
  • 举报
回复
代码没问题 ,如果你要把焦点set到一个控件上最好放在相应的函数里XXX.setFocus();
你添加focus事件只是在他获得焦点后要做的动作
islandrabbit 2009-07-07
  • 打赏
  • 举报
回复
请看一下代码:

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);
}
}


}
luoxiang2000 2009-07-07
  • 打赏
  • 举报
回复
谢谢,可是还是不对,如果你能运行以下就好了。我直接在txt2的focusGained函数中加入txt1.setfocus()都不起作用。

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧