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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值