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

📄 table1.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.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class Table1 {
	public static void main(String[] args) {
		final Display display = Display.getDefault();
		final Shell shell = new Shell();
		shell.setSize(327, 253);
		// ---------创建窗口中的其他界面组件-------------
		shell.setLayout(new FillLayout());

		// 创建一个可多选的表格,并设置它显示表头和表格线
		final Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		// 为Table增加两列并设置列宽都是80
		TableColumn col1 = new TableColumn(table, SWT.NONE);
		col1.setText("ID");
		col1.setWidth(80);
		TableColumn col2 = new TableColumn(table, SWT.NONE);
		col2.setText("姓名");
		col2.setWidth(80);

		// 创建一个Composite容器,并放入两个按钮
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new RowLayout());
		Button addButton = new Button(composite, SWT.NONE);
		addButton.setText("新增");
		addButton.addSelectionListener(new SelectionAdapter() {
			int i;

			public void widgetSelected(SelectionEvent e) {
				TableItem item = new TableItem(table, 0); // 创建Item
				item.setText(new String[] { "" + i, "陈刚" + i });// 给Item设值
				i++;
			}
		});
		Button removeButton = new Button(composite, SWT.NONE);
		removeButton.setText("删除");
		removeButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				// 得到所有被选择记录的序号,然后根据序号将记录删除
				int[] selInices = table.getSelectionIndices();
				table.remove(selInices);
			}
		});
		// 监听Table的鼠标双击事件,并显示被双击的记录
		table.addMouseListener(new MouseAdapter() {
			public void mouseDoubleClick(MouseEvent e) {
				int selIndex = table.getSelectionIndex();
				TableItem item = table.getItem(selIndex);
				String str = "列1=" + item.getText(0) + "\n列2 = " + item.getText(1);
				MessageDialog.openInformation(null, null, str);
			}
		});
		// -----------------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 + -