Java中table的用法基础篇

package test;

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.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class No5 {

 private Combo agetxt;
 private Combo sextxt;
 private Text nametxt;
 private Table table;
 protected Shell shell;

 /**
  * Launch the application
  * @param args
  */
 public static void main(String[] args) {
  try {
   No5 window = new No5();
   window.open();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * Open the window
  */
 public void open() {
  final Display display = Display.getDefault();
  createContents();
  shell.open();
  shell.layout();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }
 }

 /**
  * Create contents of the window
  */
 protected void createContents() {
  shell = new Shell();
  shell.setSize(500, 375);
  shell.setText("SWT Application");

  table = new Table(shell, SWT.BORDER);
  table.addSelectionListener(new SelectionAdapter() {
   public void widgetSelected(final SelectionEvent e) {
   
    MessageDialog.openInformation(null,"","366666");
   }
  });
  table.setLinesVisible(true);
  table.setHeaderVisible(true);
  table.setBounds(30, 38, 395, 263);

  final TableColumn uname = new TableColumn(table, SWT.NONE);
  uname.setWidth(100);
  uname.setText("姓名");

  final TableColumn sex = new TableColumn(table, SWT.NONE);
  sex.setWidth(100);
  sex.setText("性别");

  final TableColumn age = new TableColumn(table, SWT.NONE);
  age.setWidth(100);
  age.setText("年龄");

  final Button button = new Button(shell, SWT.NONE);
  button.addSelectionListener(new SelectionAdapter() {
   int j=1;
   public void widgetSelected(final SelectionEvent e) {
    TableItem Myitem=new TableItem(table,0);
    Myitem.setText(new String[]{nametxt.getText(),sextxt.getItem(sextxt.getSelectionIndex()),agetxt.getItem(agetxt.getSelectionIndex())});
    j++;
   }
  });
  button.setText("添加");
  button.setBounds(362, 9, 63, 23);

  nametxt = new Text(shell, SWT.BORDER);
  nametxt.setBounds(56, 9, 94, 25);

  this.sextxt = new Combo(shell, SWT.READ_ONLY);
  
  sextxt.setItems(new String[] {"男", "女"});
  sextxt.select(0);
  this.sextxt.setBounds(207, 9, 56, 21);

  this.agetxt = new Combo(shell, SWT.READ_ONLY);
  agetxt.setItems(new String[] {"1", "2"});
  agetxt.select(0);
  this.agetxt.setBounds(300, 11, 56, 21);

  final Label label = new Label(shell, SWT.NONE);
  label.setText("姓名");
  label.setBounds(26, 18, 38, 13);

  final Label label_1 = new Label(shell, SWT.NONE);
  label_1.setText("年龄");
  label_1.setBounds(269, 19, 25, 13);

  final Label label_2 = new Label(shell, SWT.NONE);
  label_2.setText("性别");
  label_2.setBounds(176, 17, 25, 13);
  //
 }

}

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值