【Java开源代码栏目提醒】:网学会员--在 Java开源代码编辑为广大网友搜集整理了:Combo1.java绩等信息,祝愿广大网友取得需要的信息,参考学习。
package cn.com.chengang.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);
// ---------创建窗口中的其他界面组件-------------
final Combo combo = new Combo(shell, SWT.READ_ONLY); // 定义一个只读的下拉框
combo.setBounds(16, 11, 100, 25);
// 设值按钮
Button setButton = new Button(shell, SWT.NONE);
setButton.setBounds(17, 65, 100, 25);
setButton.setText("设值");
setButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
combo.removeAll(); // 先清空combo,以防多次按下“设值”按钮时出现BUG
// combo.setItems(new String[] { "语文", "数学", "政治"});
combo.add("语文"); // 加入Combo显示值
combo.add("数学");
combo.add("政治");
combo.setData("语文", "YW");// 设置显示值的代表值
combo.setData("数学", "SX");
combo.setData("政治", "ZZ");
combo.select(1); // 设置第一项为当前项
// combo.setText("数学");
}
});
// 取值按钮
Button getButton = new Button(shell, SWT.NONE);
getButton.setBounds(136, 66, 100, 25);
getButton.setText("取值");
getButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
String str = combo.getText();// 取得显示值
String data = (String) combo.getData(str);// 取得代表值
MessageDialog.openInformation(null, null, str + data);
}
});
// -----------------END------------------------
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}