⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 combo1.java

📁 SWTJFace篇项目源程序该项目包含 包含了Eclipse下构建swt的基本工程
💻 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();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -