SWT美化版登录界面

 

package com.yc.navy.ui;

import java.awt.Dimension;

import java.awt.Toolkit;

import org.eclipse.jface.dialogs.MessageDialog;

import org.eclipse.swt.SWT;

import org.eclipse.swt.custom.CLabel;

import org.eclipse.swt.events.FocusAdapter;

import org.eclipse.swt.events.FocusEvent;

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.MouseTrackAdapter;

import org.eclipse.swt.events.SelectionAdapter;

import org.eclipse.swt.events.SelectionEvent;

import org.eclipse.swt.graphics.Color;

import org.eclipse.swt.graphics.Image;

import org.eclipse.swt.graphics.ImageData;

import org.eclipse.swt.graphics.Region;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.swt.widgets.Text;

import org.eclipse.swt.widgets.ToolBar;

import org.eclipse.swt.widgets.ToolItem;

import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.navy.efqs.util.ConstantInfo;

import org.eclipse.swt.widgets.Composite;

public class Login {

protected Shell shell;

private int clickx;

private int clicky;

private boolean checkDown = false;

private Text text;

private Text text_1;

private CLabel label; // 错误提示

public static void main(String[] args) {

try {

Login window = new Login();

window.open();

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* Open the window.

*/

public void open() {

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(SWT.NO_TRIM);

shell.setImage(SWTResourceManager.getImage(Login.class, "/images/logo.png"));

shell.setSize(810, 510);

shell.setText(ConstantInfo.TITLE + "-用户登录");

shell.setBackground(new Color(null, 236, 236, 236));

Region region = createRoundedRectangle(0, 0, 810, 510, 5);

shell.setRegion(region);

setCenter(shell);

Composite composite_parent = new Composite(shell, SWT.NONE);

composite_parent.setBounds(5, 5, 800, 500);

composite_parent.setBackgroundMode(SWT.INHERIT_DEFAULT);

composite_parent.setSize(800, 500);

composite_parent.setBackgroundImage(scaleImage(SWTResourceManager.getImage(LoginUI.class, "/images/bg.jpg"), 800, 500));

composite_parent.setRegion(createRoundedRectangle(0, 0, 800, 500, 5));

// 添加移动监听器

composite_parent.addMouseMoveListener(new MouseMoveListener() {

public void mouseMove(MouseEvent e) {

if(checkDown){

// 面板的当前位置的x轴位置+鼠标当前的位置-鼠标点击时的位置

shell.setLocation(shell.getLocation().x + e.x - clickx, shell.getLocation().y + e.y - clicky);

}

}

});

// 鼠标监听事件

composite_parent.addMouseListener(new MouseAdapter() {

@Override

public void mouseDown(MouseEvent e) { //当按下鼠标左键时

checkDown = true; //将拖动的标识设为true;

clickx = e.x; //获取鼠标当前的x轴坐标

clicky = e.y; //获取鼠标当前的y轴坐标

}

@Override

public void mouseUp(MouseEvent e) { //当松开鼠标时

checkDown = false; //将拖动的标识设为false

}

});

CLabel label_info = new CLabel(composite_parent, SWT.NONE);

label_info.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 18, SWT.BOLD));

label_info.setText("衡阳市源辰信息科技有限公司");

label_info.setAlignment(SWT.CENTER);

label_info.setBounds(106, 77, 595, 75);

Composite composite = new Composite(composite_parent, SWT.BORDER);

composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));

composite.setRegion(createRoundedRectangle(0, 0, 351, 45, 5));

composite.setBounds(237, 196, 351, 45);

CLabel label_1 = new CLabel(composite, SWT.NONE);

label_1.setAlignment(SWT.CENTER);

label_1.setImage(SWTResourceManager.getImage(LoginUI.class, "/images/user1.png"));

label_1.setBounds(0, 0, 53, 40);

text = new Text(composite, SWT.NONE);

text.setBounds(59, 10, 280, 30);

text.setText("请输入账号");

Composite composite_1 = new Composite(composite_parent, SWT.BORDER);

composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));

composite_1.setRegion(createRoundedRectangle(0, 0, 351, 45, 5));

composite_1.setBounds(237, 282, 351, 45);

CLabel label_2 = new CLabel(composite_1, SWT.NONE);

label_2.setImage(SWTResourceManager.getImage(LoginUI.class, "/images/pwd1.png"));

label_2.setAlignment(SWT.CENTER);

label_2.setBounds(0, 0, 53, 40);

text_1 = new Text(composite_1, SWT.NONE | SWT.PASSWORD);

text_1.setText("请输入密码");

text_1.setEchoChar((char)0);

text_1.setBounds(59, 10, 280, 30);

label = new CLabel(composite_parent, SWT.NONE);

label.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));

label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.BOLD));

label.setBounds(237, 342, 346, 35);

CLabel label_btn = new CLabel(composite_parent, SWT.CENTER);

label_btn.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));

label_btn.setRegion(createRoundedRectangle(0, 0, 150, 51, 5));

label_btn.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.BOLD));

label_btn.setText("登 录");

label_btn.setBackgroundImage(scaleImage(SWTResourceManager.getImage(LoginUI.class, "/images/btn.gif"), 150, 50));

label_btn.setBounds(328, 392, 150, 51);

ToolBar toolBar = new ToolBar(composite_parent, SWT.FLAT | SWT.RIGHT);

toolBar.setBounds(718, 2, 80, 40);

ToolItem item_min = new ToolItem(toolBar, SWT.NONE);

item_min.setHotImage(SWTResourceManager.getImage(LoginUI.class, "/images/min2.png"));

item_min.setImage(SWTResourceManager.getImage(LoginUI.class, "/images/min1.png"));

ToolItem item_close = new ToolItem(toolBar, SWT.NONE);

item_close.setHotImage(SWTResourceManager.getImage(LoginUI.class, "/images/close2.png"));

item_close.setImage(SWTResourceManager.getImage(LoginUI.class, "/images/close1.png"));

text.addFocusListener(new FocusAdapter() {

@Override

public void focusLost(FocusEvent e) {

String txt = text.getText().trim();

if ("".equals(txt)) {

text.setText("请输入账号");

return;

}

}

@Override

public void focusGained(FocusEvent e) {

String txt = text.getText().trim();

if ("请输入账号".equals(txt)) {

text.setText("");

return;

}

}

});

text.addKeyListener(new KeyAdapter() {

@Override

public void keyPressed(KeyEvent e) {

if (e.keyCode == 13) {

text_1.setFocus();

}

}

});

text_1.addFocusListener(new FocusAdapter() {

@Override

public void focusLost(FocusEvent e) {

String txt = text_1.getText().trim();

if ("".equals(txt)) {

text_1.setText("请输入密码");

text_1.setEchoChar((char)0);

return;

}

}

@Override

public void focusGained(FocusEvent e) {

String txt = text_1.getText().trim();

if ("请输入密码".equals(txt)) {

text_1.setText("");

text_1.setEchoChar('*');

return;

}

}

});

text_1.addKeyListener(new KeyAdapter() {

@Override

public void keyPressed(KeyEvent e) {

if (e.keyCode == 13) {

label_btn.setFocus();

label_btn.notifyListeners(SWT.MouseDown, null);

}

}

});

label_btn.addMouseTrackListener(new MouseTrackAdapter() {

@Override

public void mouseHover(MouseEvent e) {

label_btn.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));

}

@Override

public void mouseExit(MouseEvent e) {

label_btn.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));

}

});

// 登录

label_btn.addMouseListener(new MouseAdapter() {

@Override

public void mouseDown(MouseEvent e) {

String account = text.getText().trim();

if (checkNull(account) || "请输入账号".equals(account)) {

label.setText("账号不能为空...");

text.setFocus();

return;

}

String pwd = text_1.getText().trim();

if (checkNull(pwd) || "请输入密码".equals(pwd)) {

label.setText("密码不能为空...");

text_1.setFocus();

return;

}

label.setText("账号或密码错误...");

}

});

item_close.addSelectionListener(new SelectionAdapter() {

@Override

public void widgetSelected(SelectionEvent e) {

if(MessageDialog.openConfirm(shell, "关闭提示","请问您真的要关闭吗?")){

shell.dispose();

}

}

});

item_min.addSelectionListener(new SelectionAdapter() {

@Override

public void widgetSelected(SelectionEvent e) {

shell.setMinimized(true);

}

});

}

/**

* 将指定图片缩放成指定大小并返回

* @param fl

* @param width

* @param height

* @return

*/

public static Image scaleImage(Image image, int width, int height) {

ImageData imageData = image.getImageData();

imageData = imageData.scaledTo(width, height);

return new Image(null, imageData);

}

/**

* 居中显示

* @param shell

*/

public static void setCenter(Shell shell) {

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

shell.setLocation((d.getSize().width - shell.getSize().x) / 2, (d.getSize().height - shell.getSize().y) / 2 - 10);

// Display.getCurrent().getClientArea().width

}

/**

* 字符串空判断

* @param strs

* @return

*/

public static boolean checkNull(String ... strs) {

if (strs == null || strs.length <= 0) {

return true;

}

for (String str : strs) {

if (str == null || str.isEmpty()) {

return true;

}

}

return false;

}

/**

* RoundedRectangle: 圆形矩形

* @return

*/

public static Region createRoundedRectangle(int x, int y, int width, int height, int radius) {

Region region = new Region(); // 区域 身体部位

int d = (2 * radius); // diameter

region.add(circle(radius, (x + radius), (y + radius)));

region.add(circle(radius, (x + width - radius), (y + radius)));

region.add(circle(radius, (x + width - radius), (y + height - radius)));

region.add(circle(radius, (x + radius), (y + height - radius)));

region.add((x + radius), y, (width - d), height);

region.add(x, (y + radius), width, (height - d));

return region;

}

public static int[] circle(int r, int offsetX, int offsetY) {

int[] polygon = new int[8 * r + 4];

// x^2 + y^2 = r^2

for (int i = 0; i < 2 * r + 1; i++) {

int x = i - r;

int y = (int) Math.sqrt(r * r - x * x);

polygon[2 * i] = offsetX + x;

polygon[2 * i + 1] = offsetY + y;

polygon[8 * r - 2 * i - 2] = offsetX + x;

polygon[8 * r - 2 * i - 1] = offsetY - y;

}

return polygon;

}

}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值