📄 combo2.java
字号:
/**
* 作者:刘 金鑫
* Email:JinxinLiu@gmail.com
*/
package swt;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Combo1 {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(327, 253);
shell.setText("SWT Application");
//------------------新插入的界面核心代码------------------------
final Combo combo = new Combo(shell, SWT.READ_ONLY); //定义一个只读的下拉框
combo.setBounds(16, 11, 100, 25);
//设值按钮
final Button button1 = new Button(shell, SWT.NONE);
button1.setBounds(17, 65, 100, 25);
button1.setText("设值");
button1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
combo.removeAll(); //先清空combo,以防"设值"按钮多次按下时出BUG
for (int i = 1; i <= 10; i++){
//循环,赋值
combo.add("第" + i + "个字符串"); //在combo中显示的字符串
combo.setData("" + (i - 1),new Integer(i));
}
combo.select(0); //设置第一项为当前项
}
});
//取值按钮
final Button button2 = new Button(shell, SWT.NONE);
button2.setBounds(136, 66, 100, 25);
button2.setText("取值");
button2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
//根据SelectionIndex值转化成字符串后得到对应的key
String key = "" + combo.getSelectionIndex();
//根据key值得到Object(对象),注意还要用(Integer)进行类型转换
Integer selectionObj = (Integer)combo.getData(key);
// combo.getText()得到combo中当前显示的字符串
MessageDialog.openInformation(shell, null, "选择的Integer对象的值是" +selectObj.toString());
}
});
//------------------END---------------------------------------------
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -