SWT編寫QQ個人資料界面 案例 例子

/*
 * @(#)QQPersonalSetWindows.java 1.0 09/03/26
 *
 * All rights reserved.
 */
package cn.edu.tsinghua.cs.keg.andy.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * QQ個人資料輸入器
 * @author Andy Wang
 * @version 1.0 09/03/26
 * @since JDK5.0
 */
public class QQPersonalSetWindows {
 
 private final String[] items = new String[]{"個人資料","聯繫方式"};
 private final String[] sexs = new String[]{"男","女"};
 private final String[] authoritys = new String[]{"完全公開","僅好友可見","完全保密"};
 private final String[] stars = new String[]{"","水瓶座","雙魚座","牡羊座","金牛座","雙子座","巨蟹座","獅子座","處女座","天平座","天蠍座","射手座","摩羯座"};
 private final String[] shengxiaos = new String[]{"","鼠","牛","虎","兔","龍","蛇","馬","羊","猴","雞","狗","豬"};
 private final String[] bloods = new String[]{"","A型","B型","O型","AB型","其它"};
 private final String[] months = new String[]{"","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
 private final String[] days = new String[]{"","1日","2日","3日","4日","5日","6日","7日",
   "8日","9日","10日","11日","12日","13日","14日","15日","16日","17日","18日","19日",
   "20日","21日","22日","23日","24日","25日","26日","27日","28日","29日","30日","31日"};
 
 private String signTexts = "您還沒有個性簽名,趕快簽名吧!!";
 
 private StackLayout stackLayout;
 private Composite rightPersonalCom;
 private Composite rightContactCom;
 private Composite rightCom;

 private QQPersonalSetWindows(){}
 
 /**
  *
  */
 public void open(){
  
  Display display = new Display();
  
  final Shell shell = new Shell(display,SWT.BORDER|SWT.CLOSE|SWT.MIN);
  shell.setSize(500, 420);
  shell.setText("QQ個人設置");
  shell.setToolTipText("我的資料--個人設置,請填寫內容!");
  GridLayout gridLayout = new GridLayout();
  shell.setLayout(gridLayout);
  {
//          分割窗口爲左,右兩部分
   SashForm sashForm = new SashForm(shell,SWT.NONE);
   
   GridData gridData = new GridData(GridData.FILL_BOTH);
   sashForm.setLayoutData(gridData);
   {
//    分割左邊的列表框
    List selectList = new List(sashForm,SWT.BORDER);
    selectList.setItems(items);
    selectList.addMouseListener(new MyMouseListener(selectList));
   }
   {
//    右邊堆棧式面板
    stackLayout = new StackLayout();
    rightCom = new Composite(sashForm,SWT.BORDER);
    rightCom.setLayout(stackLayout);
    rightPersonalCom = this.createPersonalMsg(rightCom);
    rightContactCom = this.createContact(rightCom);
    
//    設置初始化顯示的面板
    stackLayout.topControl = rightPersonalCom;
    
   }
   sashForm.setWeights(new int[]{1,3});
//   sashForm.setBounds(20, 20, 500, 420);
  }
  {
//   按鈕面板
   Composite buttonComp = new Composite(shell,SWT.NONE);
   GridData btnGridData = new GridData();
   btnGridData.horizontalAlignment = GridData.END;
   buttonComp.setLayoutData(btnGridData);
   RowLayout btnRL = new RowLayout();
   btnRL.spacing = 20;
   buttonComp.setLayout(btnRL);
   
   Button enterBtn = new Button(buttonComp,SWT.NONE);
   enterBtn.setText("   確定    ");
   
   Button cancleBtn = new Button(buttonComp,SWT.NONE);
   cancleBtn.setText("   取消   ");
   
   Button appBtn = new Button(buttonComp,SWT.NONE);
   appBtn.setText("   應用   ");
  }
  
  shell.layout();
  shell.open();
  while(!shell.isDisposed()){
   if(!display.readAndDispatch()){
    display.sleep();
   }
  }
  display.dispose();
 }
 
 /**
  * 個人資料面板
  * @param composite
  * @return
  */
 private Composite createPersonalMsg(Composite rightComposite){
   Composite composite = new Composite(rightComposite,SWT.BORDER);
   composite.setLayout(new GridLayout(10,false));//面板採用GridLayout佈局管理器,分成六列
   Label nickName = new Label(composite,SWT.NONE);
   nickName.setText("暱稱:");
   nickName.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL,5));
  
   Label qqNum = new Label(composite,SWT.NONE);
   qqNum.setText("賬號:(綁定郵箱賬號)");
   qqNum.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL,5));
  
   Text nickText = new Text(composite,SWT.BORDER);
   nickText.setText("baby_nanhai");
   nickText.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL,5));
  
   Text numText = new Text(composite,SWT.BORDER);
   numText.setText("545073934");
   numText.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL,5));  

   Label grade = new Label(composite,SWT.NONE);
   grade.setText("等級:");
   grade.setLayoutData(this.createGridData2(GridData.HORIZONTAL_ALIGN_FILL,1));
  
   Text gradeText = new Text(composite,SWT.BORDER);
   gradeText.setText("這裏設置Image圖標");
   gradeText.setLayoutData(this.createGridData2(GridData.HORIZONTAL_ALIGN_FILL,9));
  
   Label sign = new Label(composite,SWT.NONE);
   sign.setText("個性簽名:");
   sign.setLayoutData(this.createGridData2(GridData.HORIZONTAL_ALIGN_FILL,10));
  
   Text signText = new Text(composite,SWT.BORDER|SWT.WRAP);
   signText.setText(signTexts);
   signText.setLayoutData(this.createGridData4(GridData.HORIZONTAL_ALIGN_FILL,50,10));
  
   Label sex = new Label(composite,SWT.NONE);
   sex.setText("性別:");
   sex.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL,2));
  
   Label age = new Label(composite,SWT.NONE);
   age.setText("年齡:");
   age.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL,1));
  
   Label birth = new Label(composite,SWT.NONE);
   birth.setText("生日:");
   birth.setLayoutData(this.createGridData2(GridData.HORIZONTAL_ALIGN_FILL,7));
  
   Combo sexCombo = new Combo(composite,SWT.NONE);
   sexCombo.setItems(sexs);
   sexCombo.select(0);
   sexCombo.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL,2));
  
   Text ageText = new Text(composite,SWT.BORDER);
   ageText.setText("0");
   ageText.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL,1));
  
   Combo birtMonthsCombo = new Combo(composite,SWT.NONE);
   birtMonthsCombo.setItems(months);
   birtMonthsCombo.select(0);
   birtMonthsCombo.setLayoutData(this.createGridData2(GridData.HORIZONTAL_ALIGN_FILL,3));
  
   Combo birtDaysCombo = new Combo(composite,SWT.NONE);
   birtDaysCombo.setItems(days);
   birtDaysCombo.select(0);
   birtDaysCombo.setLayoutData(this.createGridData2(GridData.HORIZONTAL_ALIGN_FILL,3));
   birtDaysCombo.setEnabled(false);
  
   Label shengxiao = new Label(composite,SWT.NONE);
   shengxiao.setText("生肖:");
   shengxiao.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL,2));
  
   Label blood = new Label(composite,SWT.NONE);
   blood.setText("血型:");
   blood.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL,1));
  
   Label starSign = new Label(composite,SWT.NONE);
   starSign.setText("星座:");
   starSign.setLayoutData(this.createGridData2(GridData.HORIZONTAL_ALIGN_FILL,7));
  
   Combo shengxiaoCombo = new Combo(composite,SWT.NONE);
   shengxiaoCombo.setItems(shengxiaos);
   shengxiaoCombo.select(0);
   shengxiaoCombo.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL,2));
  
   Combo bloodCombo = new Combo(composite,SWT.NONE);
   bloodCombo.setItems(bloods);
   bloodCombo.select(0);
   bloodCombo.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL,1));
  
   Combo starCombo = new Combo(composite,SWT.NONE);
   starCombo.setItems(stars);
   starCombo.select(0);
   starCombo.setLayoutData(this.createGridData2(GridData.HORIZONTAL_ALIGN_FILL,7));

  
  
  
  return composite;
 }
 
 /**
  * 更多資料面板編輯器
  * @param rightComposite
  * @return
  */
 private Composite createContact(Composite rightComposite){
  Composite composite = new Composite(rightComposite,SWT.BORDER);
  composite.setLayout(new GridLayout(2,false));
  
  final Label authority = new Label(composite,SWT.NONE);
  authority.setText("請選擇以下資料的顯示範圍:");
  authority.setLayoutData(this.createGridData2(GridData.HORIZONTAL_ALIGN_FILL, 1));
  
  final Label mobile = new Label(composite,SWT.NONE);
  mobile.setText("手機:");
  mobile.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL, 1));
  
  Combo authorityCombo = new Combo(composite,SWT.NONE);
  authorityCombo.setItems(authoritys);
  authorityCombo.select(2);
  authorityCombo.setLayoutData(this.createGridData2(GridData.HORIZONTAL_ALIGN_FILL,1));
  
  final Text mobileText = new Text(composite,SWT.BORDER);
  mobileText.setText("-");
  mobileText.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL,1));
  
  final Label telephone = new Label(composite,SWT.NONE);
  telephone.setText("電話:");
  telephone.setLayoutData(this.createGridData2(GridData.HORIZONTAL_ALIGN_FILL, 1));
  
  final Label email = new Label(composite,SWT.NONE);
  email.setText("郵箱:");
  email.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL, 1));
  
  final Text telephoneText = new Text(composite,SWT.BORDER);
  telephoneText.setText("-");
  telephoneText.setLayoutData(this.createGridData2(GridData.HORIZONTAL_ALIGN_FILL,1));
  
  final Text emailText = new Text(composite,SWT.BORDER);
  emailText.setText("-");
  emailText.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL,1));
  
  final Label job = new Label(composite,SWT.NONE);
  job.setText("職業:");
  job.setLayoutData(this.createGridData2(GridData.HORIZONTAL_ALIGN_FILL, 1));
  
  final Label college = new Label(composite,SWT.NONE);
  college.setText("畢業院校:");
  college.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL, 1));
  
  final Text jobText = new Text(composite,SWT.BORDER);
  jobText.setText("-");
  jobText.setLayoutData(this.createGridData2(GridData.HORIZONTAL_ALIGN_FILL,1));
  
  final Text collegeText = new Text(composite,SWT.BORDER);
  collegeText.setText("-");
  collegeText.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL,1));
  
  final Label page = new Label(composite,SWT.NONE);
  page.setText("個人主頁:");
  page.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL, 2));
  
  final Text pageText = new Text(composite,SWT.BORDER);
  pageText.setText("-");
  pageText.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL,2));
  
  final Label introduce = new Label(composite,SWT.NONE);
  introduce.setText("個人說明:");
  introduce.setLayoutData(this.createGridData2(GridData.FILL_HORIZONTAL, 2));
  
  final Text introduceText = new Text(composite,SWT.BORDER);
  introduceText.setText("-");
  introduceText.setLayoutData(this.createGridData4(GridData.FILL_HORIZONTAL,70,2));
  
  return composite;
 }
 
 /**
  *
  * @param horizontalSpan
  * @return
  */
 private GridData createGridData1(int horizontalSpan){
  GridData gridData = new GridData();
  gridData.horizontalSpan = horizontalSpan;
//  gridData.verticalSpan = verticalSpan;
  return gridData;
 }
 
 /**
  * 創建GridData
  * @param stytle 風格
  * @param horizontalSpan 合併列數
  * @return
  */
 private GridData createGridData2(int stytle,int horizontalSpan){
  GridData gridData = new GridData(stytle);
  gridData.horizontalSpan = horizontalSpan;
//  gridData.horizontalAlignment = GridData.CENTER;
  return gridData;
 }
 
 /**
  * 創建GridData
  * @param stytle 風格
  * @param horizontalSpan 合併列數
  * @param verticalSpan 合併行數
  * @return
  */
 private GridData createGridData3(int stytle,int horizontalSpan,int verticalSpan){
  GridData gridData = new GridData(stytle);
  gridData.horizontalSpan = horizontalSpan;
  gridData.verticalSpan = verticalSpan;
  
  return gridData;
 }
 
 /**
  *
  * @param stytle
  * @param heightHint
  * @param horizontalSpan
  * @return
  */
 private GridData createGridData4(int stytle,int heightHint,int horizontalSpan){
  GridData gridData = new GridData(stytle);
  gridData.heightHint = heightHint;
  gridData.horizontalSpan = horizontalSpan;
  return gridData;
 }
 
 /**
  * 鼠標單擊事件
  * @author Andy Wang
  *
  */
 private final class MyMouseListener extends MouseAdapter{

  private List list;
  public MyMouseListener(){}
  public MyMouseListener(List list){
   this.list = list;
  }
  
  public void mouseDown(MouseEvent e) {
   int selectIndex = list.getSelectionIndex();
   if(selectIndex == 0){
    stackLayout.topControl = rightPersonalCom;
    rightCom.layout();
   }else{
    stackLayout.topControl = rightContactCom;
    rightCom.layout();
   }
  }  
 }
 
 /**
  * main start QQ
  * @param args
  */
 public static void main(String[] args){
  try {
   QQPersonalSetWindows qq = new QQPersonalSetWindows();
   qq.open();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章