📄 login.java
字号:
import java.awt.Toolkit;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
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;
import myEncryption.MyEncryption;
public class Login{
private Label label_3;
private Label label_2;
private Label label_1;
private Label label;
private Button button_3;
private Button button_1;
private Button button;
private Button button_2;
private Text text_1;
private Text text;
private MessageBox messageBox;
private String id;
private String pwd;
private DBCon dbc;
private Connection con;
private int mystyle = SWT.MIN | SWT.MAX & SWT.MIN|SWT.CLOSE;
/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
Login window = new Login();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
public void setCenter(Shell shell){
// 得到屏幕的宽度和高度
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
// 得到Shell窗口的宽度和高度
int shellHeight = shell.getBounds().height;
int shellWidth = shell.getBounds().width;
// 如果窗口大小超过屏幕大小,让窗口与屏幕等大
if (shellHeight > screenHeight)
shellHeight = screenHeight;
if (shellWidth > screenWidth)
shellWidth = screenWidth;
// 让窗口在屏幕中间显示
shell.setLocation(((screenWidth - shellWidth) / 2),
((screenHeight - shellHeight) / 2));
}
/**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
final Shell shell = new Shell(mystyle);
shell.setImage(SWTResourceManager.getImage(Login.class, "id.gif"));
shell.setBackgroundImage(SWTResourceManager.getImage(Login.class, "new.jpg"));
shell.setSize(708, 586);
shell.setText("登 录");
setCenter(shell);
shell.open();
final Composite composite = new Composite(shell, SWT.NONE);
composite.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
composite.setBounds(140, 300, 426, 158);
button_2 = new Button(composite, SWT.RADIO);
button_2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
button.setFocus();
}
});
button_2.setFont(SWTResourceManager.getFont("宋体", 10, SWT.NONE));
button_2.setText("工作人员");
button_2.setBounds(85, 121, 69, 16);
button_3 = new Button(composite, SWT.RADIO);
button_3.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
button.setFocus();
}
});
button_3.setFont(SWTResourceManager.getFont("宋体", 10, SWT.NONE));
button_3.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
button_3.setText("管理员");
button_3.setBounds(160, 121, 69, 16);
button_2.setSelection(true);
text = new Text(composite, SWT.BORDER);
text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
text.addTraverseListener(new TraverseListener() {
public void keyTraversed(final TraverseEvent e) {
button.setFocus();
}
});
text.setFont(SWTResourceManager.getFont("Times New Roman", 10, SWT.NONE));
text.setBounds(99, 78, 97, 20);
text_1 = new Text(composite, SWT.PASSWORD | SWT.BORDER);
text_1.setTextLimit(20);
text_1.setEchoChar('\b');
text_1.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
text_1.addTraverseListener(new TraverseListener() {
public void keyTraversed(final TraverseEvent e) {
button.setFocus();
}
});
text_1.setBounds(279, 78, 97, 20);
label = new Label(composite, SWT.NONE);
label.setForeground(SWTResourceManager.getColor(255, 255, 255));
label.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
label.setAlignment(SWT.RIGHT);
label.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NONE));
label.setText("帐号:");
label.setBounds(40, 81, 57, 17);
label_1 = new Label(composite, SWT.NONE);
label_1.setForeground(SWTResourceManager.getColor(255, 255, 255));
label_1.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
label_1.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NONE));
label_1.setAlignment(SWT.RIGHT);
label_1.setText("密码:");
label_1.setBounds(220, 81, 57, 17);
button = new Button(composite, SWT.NONE);
button.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e)
{
shell.setEnabled(false);
id = text.getText();
pwd = text_1.getText();
if(id.length() == 0 || pwd.length() == 0 )
{
messageBox = new MessageBox(shell,SWT.ICON_INFORMATION);
messageBox.setMessage("用户名或密码不能为空");
messageBox.setText("登录失败");
messageBox.open();
}
else
if(pwd.contains(" ") )
{
messageBox = new MessageBox(shell,SWT.ICON_INFORMATION);
messageBox.setMessage("密码不能包含空格");
messageBox.setText("密码格式错误");
messageBox.open();
}
else
if(button_2.getSelection())
{
shell.setVisible(false);
staffCheck(shell,id,pwd);
}
else
if(button_3.getSelection())
{
shell.setVisible(false);
adminCheck(shell,id,pwd);
}
else
{
messageBox = new MessageBox(shell,SWT.ICON_INFORMATION);
messageBox.setMessage("请选择您的身份");
messageBox.setText("错误提示");
messageBox.open();
}
shell.setEnabled(true);
}
});
button.setText("登 录");
button.setBounds(255, 118, 57, 22);
button_1 = new Button(composite, SWT.NONE);
button_1.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
button_1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
shell.close();
System.exit(0);
}
});
button_1.setText("退 出");
button_1.setBounds(330, 118, 57, 22);
text.setFocus();
label_2 = new Label(composite, SWT.NONE);
label_2.setForeground(SWTResourceManager.getColor(255, 255, 255));
label_2.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
label_2.setAlignment(SWT.CENTER);
label_2.setFont(SWTResourceManager.getFont("黑体", 36, SWT.NONE));
label_2.setText("销售信息");
label_2.setBounds(15, 20, 192, 48);
label_3 = new Label(composite, SWT.NONE);
label_3.setForeground(SWTResourceManager.getColor(255, 255, 255));
label_3.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
label_3.setAlignment(SWT.CENTER);
label_3.setFont(SWTResourceManager.getFont("黑体", 36, SWT.NONE));
label_3.setText("管理系统");
label_3.setBounds(213, 20, 192, 48);
final Label label_4 = new Label(shell, SWT.NONE);
label_4.setText("工作人员登录帐号:\n10000 密码00000\n10003 密码33333\n10007 密码00000\n管理员登录帐号:sky 密码123456");
label_4.setBounds(358, 464, 344, 89);
composite.setTabList(new Control[] {text, text_1, button, button_1});
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
public boolean staffCheck(final Shell shell,String id,String pwd){
final Display display = Display.getDefault();
dbc = new DBCon();
try{
con = DriverManager.getConnection(dbc.url,"staff","123");
dbc.dbClose(con);
int staffType = staffLogin(id,pwd);
if(staffType != 32767){
try {
switch(staffType)
{
case 1:
Seller shell_1 = new Seller(display,mystyle,"staff","123",id);
shell_1.open();
shell_1.layout();
shell_1.addDisposeListener(new DisposeListener(){
public void widgetDisposed(final DisposeEvent e){
System.exit(0);
}
});
break;
case 2:
Servicer shell_2 = new Servicer(display,mystyle,"staff","123",id);
shell_2.open();
shell_2.layout();
shell_2.addDisposeListener(new DisposeListener(){
public void widgetDisposed(final DisposeEvent e){
System.exit(0);
}
});
break;
case 3:
Manage shell_3 = new Manage(display,mystyle,"staff","123",id);
shell_3.open();
shell_3.layout();
shell_3.addDisposeListener(new DisposeListener(){
public void widgetDisposed(final DisposeEvent e){
System.exit(0);
}
});
break;
}
} catch (Exception e_shell_1) {
shell.setVisible(true);
messageBox = new MessageBox(shell,SWT.ICON_ERROR);
messageBox.setMessage("数据库初始化失败\n" +
"请重新登录");
messageBox.setText("登录失败");
messageBox.open();
return false;
}
}
else
{
throw new Exception();
}
return true;
}
catch(Exception e_dbc)
{
shell.setVisible(true);
messageBox = new MessageBox(shell,SWT.ICON_ERROR);
messageBox.setMessage("数据库连接失败\n" +
"请检查用户名或密码是否正确");
messageBox.setText("登录失败");
messageBox.open();
return false;
}
}
public boolean adminCheck(final Shell shell,String id,String pwd){
final Display display = Display.getDefault();
dbc = new DBCon();
try{
con = DriverManager.getConnection(dbc.url,id,pwd);
dbc.dbClose(con);
text.setText("");
text_1.setText("");
try {
Admin shell_2 = new Admin(display,mystyle,id,pwd);
shell_2.open();
shell_2.layout();
shell_2.addDisposeListener(new DisposeListener(){
public void widgetDisposed(final DisposeEvent e){
System.exit(0);
}
});
} catch (Exception e_shell_2) {
e_shell_2.printStackTrace();
shell.setVisible(true);
messageBox = new MessageBox(shell,SWT.ICON_ERROR);
messageBox.setMessage("数据库连接失败\n" +
"请重新登录");
messageBox.setText("登录失败");
messageBox.open();
return false;
}
return true;
}
catch(Exception e_dbc)
{
shell.setVisible(true);
messageBox = new MessageBox(shell,SWT.ICON_ERROR);
messageBox.setMessage("数据库连接失败\n" +
"请检查用户名或密码是否正确");
messageBox.setText("登录失败");
messageBox.open();
return false;
}
}
public int staffLogin(String id,String pwd)
{
int result = 32767;
Connection con;
Statement stmt;
MyEncryption me = new MyEncryption();
pwd = String.valueOf(me.passwordMaker(pwd));
try{
DBCon dbc = new DBCon();
con = DriverManager.getConnection(dbc.url,"staff","123");
stmt = con.createStatement();
String query ;
ResultSet rs ;
/**
* 实现sql和程序的字符转换
*/
//向转换表插入数据
query = "update Change " +
"set ExChange = '"+ pwd +"';";
stmt.executeUpdate(query);
//读出插入后的数据
query = "select ExChange from Change ;";
rs = stmt.executeQuery(query);
while( rs.next()){
pwd = rs.getString("ExChange");
}
if(rs != null)
rs.close();
/**
* 将转换后的密码与Staff表中的密码比较
*/
query = "select Password,Type from Staff where ID = '"+id+"';";
rs = stmt.executeQuery(query);
while( rs.next()){
String temp = rs.getString("Password");
char[] pwdtemp = temp.toCharArray();
char[] c_pwd = pwd.toCharArray();
if(pwdtemp.length == c_pwd.length)
{
int i = 0;
for(;i < pwdtemp.length; i++)
{
if(pwdtemp[i] != c_pwd[i])
{
break;
}
}
if(i == pwdtemp.length)
result = Integer.parseInt(rs.getString("Type"));
}
}
dbc.dbClose(con,rs);
}
catch(Exception e){
e.printStackTrace();
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -