一、使用JAVA_SWT编写登陆界面(不含数据库,登陆按钮未处理)

一、为了进行SWT应用开发,你需要把SWT库添加到类路径(classpath)上,并设置必要的环境变量。   

         首先,安装及配置swt+design

         1.1  下载并解压Designer_v6.9.5_for_Eclipse3.5.zip,把features和plugins覆盖到eclipse下同名文档,并把configuration文件夹下的除config.ini外的文件全部删除。

         1.2  打开WindowBuilderKeygen.exe程序,填写正确网卡物理地址和版本号后,点击OK,得到序列号及注册码。

         1.3  打开Eclipse,在主菜单下选择window=>Preferences=>Designer=>License=>Registration and Activation,输入序列号与注册后,点下一步按要求填写完成(Name中间有空格),最后OK(如下图)。

         1.4  取消Automatically activate when possible选项。

 

 

         然后。你要在ECLIPSE_HOM=》Eeclipse=》plugins文件夹下找到org.eclipse.swt.win32.win32.x86_3.3.0.v3346.jar库文件。注意这里的“org.eclipse.swt.win32.win32.x86_3.3.0.v3346.jar”是和Eclipse的版本有关的。实在找不到你就用文件搜索功能吧。然后依次打开下面窗口Project-〉Properies-〉JavaBuildPath-〉Libraries-〉Add Variable -〉 Eclipse Home -〉Extend将org.eclipse.swt.win32.win32.x86_3.3.0.v3346.jar文件加到类路径中。

 

 

二、拖拉控件,输入代码(界面如下)

功能说明:

1、限制文本框只能输入小写字母和数字

2、限制文本框字符个数为8位

3、实现退出按钮功能

4、禁止拖动,修改界面大小

5、实现CANVAS控件内图片按比例缩放显示并响应鼠标释放事件

 

import org.eclipse.swt.SWT; import org.eclipse.swt.events.KeyAdapter; import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseMoveListener; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; 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.swtdesigner.SWTResourceManager; public class test { private Text Text_password; private Text Text_account; protected Shell shell; /** * Launch the application * * @param args */ public static void main(String[] args) { try { test window = new test(); 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(); } display.dispose(); } /** * Create contents of the window */ protected void createContents() { shell = new Shell(SWT.NO_TRIM); shell.addMouseMoveListener(new MouseMoveListener() { public void mouseMove(final MouseEvent arg0) { MessageBox mb = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL); mb.setText("提示"); mb.setMessage("确定要关闭吗?"); } }); shell.setSize(360, 196); // 取屏幕大小 Rectangle src_area = Display.getDefault().getClientArea(); // 使窗口居中显示 shell.setLocation((src_area.width - 360) / 2, (src_area.height - 196) / 2); // 创建"帐号"标签 final Label Lab_account = new Label(shell, SWT.CENTER); Lab_account.setAlignment(SWT.CENTER); Lab_account.setForeground(Display.getCurrent().getSystemColor( SWT.COLOR_BLACK)); Lab_account.setFont(SWTResourceManager.getFont("", 12, SWT.NONE)); Lab_account.setText("帐号:"); Lab_account.setBounds(81, 42, 48, 25); // 创建"密码"标签 final Label Lab_password = new Label(shell, SWT.CENTER); Lab_password.setBounds(80, 82, 48, 25); Lab_password.setFont(SWTResourceManager.getFont("", 12, SWT.NONE)); Lab_password.setText("密码:"); // 创建"帐号"文本框 Text_account = new Text(shell, SWT.BORDER); //限制只能输入8个字符 Text_account.setTextLimit(8); Text_account.setToolTipText("只允许输入数字以及小写字母"); // 限制输入字符 Text_account.addKeyListener(new KeyAdapter() { public void keyPressed(final KeyEvent e) { if (!CheckTextData(e.character)) e.doit = false; } }); Text_account.setFont(SWTResourceManager.getFont("", 12, SWT.NONE)); Text_account.setBounds(138, 40, 111, 25); // 创建"密码"文本框 Text_password = new Text(shell, SWT.BORDER | SWT.PASSWORD); //限制输入8个字符 Text_password.setTextLimit(8); Text_password.setToolTipText("只允许输入数字以及小写字母"); // 限制输入字符 Text_password.addKeyListener(new KeyAdapter() { public void keyPressed(final KeyEvent e) { if (!CheckTextData(e.character)) e.doit = false; } }); Text_password.setFont(SWTResourceManager.getFont("", 12, SWT.NONE)); Text_password.setBounds(138, 82, 111, 25); // 创建"登陆"按钮 final Button Btn_login = new Button(shell, SWT.NONE); //设置缺省按钮 shell.setDefaultButton(Btn_login); Btn_login.setText("登陆"); Btn_login.setBounds(105, 135, 48, 22); // 创建"退出"按钮 final Button Btn_quit = new Button(shell, SWT.NONE); // “退出”按钮事件 Btn_quit.addSelectionListener(new SelectionAdapter() { public void widgetSelected(final SelectionEvent e) { MessageBox mb = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL); mb.setText("提示"); mb.setMessage("确定要关闭吗?"); int rc = mb.open(); if (e.doit == (rc == SWT.OK)) { // 做窗口关闭事件 shell.close(); } else { // 什么也不做 } } }); Btn_quit.setText("退出"); Btn_quit.setBounds(200, 135, 48, 22); // 创建配置图片 final Canvas canvas = new Canvas(shell, SWT.NONE); canvas.setBounds(265, 108, 79, 71); Rectangle rect_can = canvas.getBounds(); ImageData Data_img = new ImageData("Res//Setting.png").scaledTo( rect_can.width, rect_can.height); final Image myImage = new Image(Display.getDefault(), Data_img); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { e.gc.drawImage(myImage, 0, 0); } }); /** * Canvas获得焦点响应 canvas.addKeyListener(new KeyAdapter() { public void * keyPressed(final KeyEvent e) { * System.out.println("这事件不知为何,为下面事件做铺垫"); } }); * canvas.addFocusListener(new FocusAdapter() { public void * focusGained(final FocusEvent e) { System.out.println("sdfsd"); } }); */ // CANVAS中鼠标事件 canvas.addMouseListener(new MouseAdapter() { //鼠标释放 public void mouseUp(final MouseEvent e) { System.out.println("Up"); } }); } // 文本框输入内容检查,只允许小写字母和数字 public boolean CheckTextData(char ch) { if (Character.isDigit(ch) || ch == '/b' || Character.isLowerCase(ch)) { return true; } return false; } }  



 

你可能感兴趣的:(一、使用JAVA_SWT编写登陆界面(不含数据库,登陆按钮未处理))