SWT开发 - Combo的使用(2) - 事件处理
2008-01-26 00:17Update
本文介绍SWT Combo的事件处理方法。
范例代码
ComboSample02.javapackage com.test.swt;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class ComboSample02 {
public static void main(String[] args){
Display display = new Display ();
final Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
// read only style Combo
Combo c3 = new Combo(shell,SWT.READ_ONLY);
c3.add("c3-1", 0);
c3.add("c3-2", 1);
c3.select(0);
final Text l = new Text(shell, SWT.NULL);
c3.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
setSelectedValue(
((Combo)e.widget).getSelectionIndex());
}
public void widgetSelected(SelectionEvent e) {
setSelectedValue(
((Combo)e.widget).getSelectionIndex());
}
private void setSelectedValue(int value) {
l.setText(String.valueOf(value));
}
});
shell.pack();
shell.open();
while (!shell.isDisposed ()){
if (!display.readAndDispatch ()){
display.sleep ();
}
}
display.dispose ();
}
}
解说
创建Combo控件之后,可以通过
c3.addSelectionListener(new SelectionListener() {...
为其指定“选择”操作事件的处理机,该处理机需要实现SelectionListener的几个方法。
本例通过((Combo)e.widget).getSelectionIndex()取得被选择的项目的index值,并设置到Text l加以显示。
画面显示
- Relative Articles
- SWT开发 - Text的使用(2) - 事件处理 - (2008-01-26 00:38)
- SWT开发 - Text的使用(1) - (2008-01-26 00:29)
- SWT开发 - Combo的使用(1) - (2008-01-26 00:10)
- SWT开发 - Button的使用(3) - Toggle风格的按钮 - (2008-01-23 00:47)
- SWT开发 - Button的使用(2) - Radio按钮 - (2008-01-23 00:42)
- SWT开发 - Button的使用(1) - (2008-01-23 00:28)
- SWT开发 - Label的使用(3) - (2008-01-22 00:36)
- SWT开发 - Label的使用(2) - (2008-01-22 00:32)
- SWT开发 - 窗口(Window)显示 - (2008-01-22 00:22)
- SWT开发 - Label的使用(1) - (2008-01-22 00:21)