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);
  //
 }

}

 

你可能感兴趣的:(Java学习笔记)