Java EE SWT 註冊界面

FormLayoutGUI.Java

package com.demo.layout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.util.SWTUtil;

public class FormLayoutGUI {
 public static final int  H_GAP = 20;
 private Shell shell;
 private FormLayout layout;
 private Label lab_title;
 private FormData data;
 private Label lab_username;
 private Text txt_username;
 private Label lab_username_info;
 private Label lab_password;
 private Text txt_password;
 private Label lab_password_info;
 private Text txt_reppassword;
 private Label lab_reppassword;
 private Label lab_reppassword_info;
 private Label lab_sex;
 private Button but_sex_male;
 private Label lab_sex_male;
 private Button but_sex_female;
 private Label lab_sex_female;
 private Label lab_country;
 private Combo com_country;
 private Label lab_province;
 private Combo com_province;
 private Label lab_interest;
 private Button but_interest_dance;
 private Button but_interest_music;
 private Button but_interest_swim;
 private Button but_interest_read;
 private Label lab_intro;
 private Text txt_intro;
 private Button but_reg;
 
 public void init(){
  shell = SWTUtil.getShell();
  shell.setText("用戶註冊");
  shell.setSize(500, 620);
  
  //定義佈局管理器,並設置窗口的內邊距
  layout = new FormLayout();
  layout.marginWidth = 20;
  layout.marginHeight = 20;
  shell.setLayout(layout);
  //標題Label
  lab_title = new Label(shell,SWT.NONE);
  lab_title.setText("用戶註冊");
  lab_title.setFont(new Font(null,"隸書",24,SWT.NONE));
  lab_title.setForeground(new Color(null,200,100,0));
  data = new FormData();
  data.top = new FormAttachment(2,0);
  data.left = new FormAttachment(38,0);
  lab_title.setLayoutData(data);
  
  //用戶名Label
  lab_username = new Label(shell,SWT.NONE);
  lab_username.setText("用 戶 名:");
  data = new FormData();
  data.top = new FormAttachment(lab_title,H_GAP);
  data.left = new FormAttachment(0,5);
  lab_username.setLayoutData(data);
  //用戶名輸入框
  txt_username = new Text(shell,SWT.BORDER);
  data = new FormData();
  data.top = new FormAttachment(lab_username,0,SWT.CENTER);
  data.left = new FormAttachment(lab_username,10);
  data.right = new FormAttachment(lab_username,200,SWT.RIGHT);
  txt_username.setLayoutData(data);
  
  txt_username.addFocusListener(new FocusListener()
  {
   @Override
   public void focusGained(FocusEvent evt) {
    lab_username_info.setForeground(new Color(null,0,0,0));
    lab_username_info.setText("用戶名必須爲6-16位字母、數字!");
   }

   @Override
   public void focusLost(FocusEvent evt) {
    if(txt_username.getText().trim().equals("")){
     lab_username_info.setForeground(new Color(null,255,0,0));
     lab_username_info.setText("用戶名不能爲空!");
    }else{
     lab_username_info.setForeground(new Color(null,0,255,0));
     lab_username_info.setText("用戶名格式正確!");
    }
   }
   
  });
  
  //用戶名輸入提示Label
  lab_username_info = new Label(shell,SWT.NONE);
  lab_username_info.setText("用戶名必須爲6-16位字母、數字!");
  data = new FormData();
  data.top = new FormAttachment(txt_username,0,SWT.CENTER);
  data.left = new FormAttachment(txt_username,10);
  data.right = new FormAttachment(txt_username,200,SWT.RIGHT);
  lab_username_info.setLayoutData(data);
  //密碼Label
  lab_password = new Label(shell,SWT.NONE);
  lab_password.setText("密      碼:");
  data = new FormData();
  data.top = new FormAttachment(lab_username,H_GAP);
  data.left = new FormAttachment(lab_username,0,SWT.LEFT);
  lab_password.setLayoutData(data);
  //密碼輸入框
  txt_password = new Text(shell,SWT.BORDER|SWT.PASSWORD);
  data = new FormData();
  data.top = new FormAttachment(lab_password,0,SWT.CENTER);
  data.left = new FormAttachment(txt_username,0,SWT.LEFT);
  data.right = new FormAttachment(txt_username,0,SWT.RIGHT);
  txt_password.setLayoutData(data);
  //密碼輸入提示Label
  lab_password_info = new Label(shell,SWT.NONE);
  lab_password_info.setText("密碼不能爲空!");
  data = new FormData();
  data.top = new FormAttachment(txt_password,0,SWT.CENTER);
  data.left = new FormAttachment(txt_password,10);
  lab_password_info.setLayoutData(data);
  //重複密碼Label
  lab_reppassword = new Label(shell,SWT.NONE);
  lab_reppassword.setText("密碼重複:");
  data = new FormData();
  data.top = new FormAttachment(lab_password,H_GAP);
  data.left = new FormAttachment(lab_password,0,SWT.LEFT);
  lab_reppassword.setLayoutData(data);
  //密碼重複輸入框
  txt_reppassword = new Text(shell,SWT.BORDER|SWT.PASSWORD);
  data = new FormData();
  data.top = new FormAttachment(lab_reppassword,0,SWT.CENTER);
  data.left = new FormAttachment(txt_password,0,SWT.LEFT);
  data.right = new FormAttachment(txt_password,0,SWT.RIGHT);
  txt_reppassword.setLayoutData(data);
  //密碼重複輸入提示Label
  lab_reppassword_info = new Label(shell,SWT.NONE);
  lab_reppassword_info.setText("兩次密碼必須一致!");
  data = new FormData();
  data.top = new FormAttachment(txt_reppassword,0,SWT.CENTER);
  data.left = new FormAttachment(txt_reppassword,10);
  lab_reppassword_info.setLayoutData(data);
  //性別Label
  lab_sex = new Label(shell,SWT.NONE);
  lab_sex.setText("性      別:");
  data = new FormData();
  data.top = new FormAttachment(lab_reppassword,H_GAP);
  data.left = new FormAttachment(lab_reppassword,0,SWT.LEFT);
  lab_sex.setLayoutData(data);
  //性別選擇按鈕:男
  but_sex_male = new Button(shell,SWT.RADIO);
  data = new FormData();
  data.top = new FormAttachment(lab_sex,0,SWT.CENTER);
  data.left = new FormAttachment(txt_reppassword,0,SWT.LEFT);
  but_sex_male.setLayoutData(data);
  lab_sex_male = new Label(shell,SWT.NONE);
  //lab_sex_male.setText("男");
  lab_sex_male.setImage(new Image(null,new ImageData("pic\\male.JPG")));
  data = new FormData();
  data.top = new FormAttachment(but_sex_male,0,SWT.CENTER);
  data.left = new FormAttachment(but_sex_male,5);
  lab_sex_male.setLayoutData(data);
  //性別選擇按鈕:女
  but_sex_female = new Button(shell,SWT.RADIO);
  data = new FormData();
  data.top = new FormAttachment(lab_sex_male,0,SWT.CENTER);
  data.left = new FormAttachment(lab_sex_male,20);
  but_sex_female.setLayoutData(data);
  lab_sex_female = new Label(shell,SWT.NONE);
  //lab_sex_female.setText("女");
  lab_sex_female.setImage(new Image(null,new ImageData("pic\\female.JPG")));
  data = new FormData();
  data.top = new FormAttachment(but_sex_female,0,SWT.CENTER);
  data.left = new FormAttachment(but_sex_female,5);
  lab_sex_female.setLayoutData(data);
  
  //所屬國家Label
  lab_country = new Label(shell,SWT.NONE);
  lab_country.setText("國      家:");
  data = new FormData();
  data.top = new FormAttachment(lab_sex,H_GAP);
  data.left = new FormAttachment(lab_sex,0,SWT.LEFT);
  lab_country.setLayoutData(data);
  //國家下拉列表框
  com_country = new Combo(shell,SWT.NONE);
  String[] countries = {"中國","美國","英國","加拿大"};
  com_country.setItems(countries);
  com_country.select(0);
  data = new FormData();
  data.top = new FormAttachment(lab_country,0,SWT.CENTER);
  data.left = new FormAttachment(lab_sex,10);
  com_country.setLayoutData(data);
  //所屬省份Label
  lab_province = new Label(shell,SWT.NONE);
  lab_province.setText("省      份:");
  data = new FormData();
  data.top = new FormAttachment(com_country,0,SWT.CENTER);
  data.left = new FormAttachment(com_country,20);
  lab_province.setLayoutData(data);
  //省份下拉列表框
  com_province = new Combo(shell,SWT.NONE);
  String[] provinces = {"山西","湖南","北京","上海"};
  com_province.setItems(provinces);
  com_province.select(0);
  data = new FormData();
  data.top = new FormAttachment(lab_province,0,SWT.CENTER);
  data.left = new FormAttachment(lab_province,10);
  data.right = new FormAttachment(lab_province,80,SWT.RIGHT);
  com_province.setLayoutData(data);
  
  //興趣愛好Label
  lab_interest = new Label(shell,SWT.NONE);
  lab_interest.setText("興趣愛好:");
  data = new FormData();
  data.top = new FormAttachment(lab_country,H_GAP);
  data.left = new FormAttachment(lab_country,0,SWT.LEFT);
  lab_interest.setLayoutData(data);
  //興趣複選框:跳舞
  but_interest_dance = new Button(shell,SWT.CHECK);
  but_interest_dance.setText("跳舞");
  data = new FormData();
  data.top = new FormAttachment(lab_interest,0,SWT.CENTER);
  data.left = new FormAttachment(lab_country,10);
  but_interest_dance.setLayoutData(data);
  //興趣複選框:聽音樂
  but_interest_music = new Button(shell,SWT.CHECK);
  but_interest_music.setText("聽音樂");
  data = new FormData();
  data.top = new FormAttachment(but_interest_dance,0,SWT.CENTER);
  data.left = new FormAttachment(but_interest_dance,20);
  but_interest_music.setLayoutData(data);
  //興趣複選框:游泳
  but_interest_swim = new Button(shell,SWT.CHECK);
  but_interest_swim.setText("游泳");
  data = new FormData();
  data.top = new FormAttachment(but_interest_music,0,SWT.CENTER);
  data.left = new FormAttachment(but_interest_music,20);
  but_interest_swim.setLayoutData(data);
  //興趣複選框:閱讀
  but_interest_read = new Button(shell,SWT.CHECK);
  but_interest_read.setText("閱讀");
  data = new FormData();
  data.top = new FormAttachment(but_interest_swim,0,SWT.CENTER);
  data.left = new FormAttachment(but_interest_swim,20);
  but_interest_read.setLayoutData(data);
  
  //個人簡介Label
  lab_intro = new Label(shell,SWT.NONE);
  lab_intro.setText("個人簡介:");
  data = new FormData();
  data.top = new FormAttachment(lab_interest,H_GAP);
  data.left = new FormAttachment(lab_interest,0,SWT.LEFT);
  lab_intro.setLayoutData(data);
  //個人簡介輸入框
  txt_intro = new Text(shell,SWT.BORDER|SWT.MULTI|SWT.WRAP);
  txt_intro.setText("這個傢伙很懶,什麼都沒有留下!");
  data = new FormData();
  data.top = new FormAttachment(lab_intro,0,SWT.TOP);
  data.left = new FormAttachment(lab_interest,10);
  data.right = new FormAttachment(lab_interest,300,SWT.RIGHT);
  data.bottom = new FormAttachment(85,15);
  txt_intro.setLayoutData(data);
  
  //註冊按鈕
  but_reg = new Button(shell,SWT.PUSH|SWT.FLAT);
  but_reg.setText("注    冊");
  data = new FormData();
  data.top = new FormAttachment(txt_intro,H_GAP);
  data.left = new FormAttachment(txt_intro,100,SWT.LEFT);
  data.right = new FormAttachment(txt_intro,-100,SWT.RIGHT);
  but_reg.setLayoutData(data);
  but_reg.addMouseListener(new MouseAdapter(){
   public void mouseDown(MouseEvent evt) {
    MessageBox mBox = new MessageBox(shell,SWT.ICON_INFORMATION);
    mBox.setText("註冊成功");
    mBox.setMessage("恭喜你,註冊成功!");
    mBox.open();
   }
  });
  
  SWTUtil.openShell(shell);
 }
 //step1:創建事件監聽器類
 
 public static void main(String[] args) {
  FormLayoutGUI gui = new FormLayoutGUI();
  gui.init();
 }
}

 

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