62,635
社区成员




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