按钮响应之“登录”
package com.net.action;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
//import com.net.convert.MakeCertPic;
import com.net.convert.Security;
import com.net.ui.LandWindow;
public class Submit implements ActionListener{
private LandWindow landUI=null;
private String name;
private String pswd;
private String IP=null;
private int SERVERPORT=-1;
private String SPLITSTR=null;
private OutputStream os=null;
private InputStream is=null;
private final String respondStyle="land";
private final String ISACK = "ACK";
private final String ISNAK = "NAK!";
public Submit(LandWindow landUI) {
this.landUI=landUI;
this.landUI.refreshCertPicLabel();
}
@Override
public void actionPerformed(ActionEvent arg0) {
String name = landUI.getUserNameField().getText();
String pswd = new String(landUI.getPasswdField().getPassword());
String certStr = landUI.getCertTextField().getText();
Security security = new Security(pswd);
security.securityAlgorithm("MD5");
boolean passFlag=true;
landUI.getUserNameLabel().setText(null);
landUI.getPasswdLabel().setText(null);
landUI.getStatLabel().setText(null);
if (name.length() == 0){
landUI.getUserNameLabel().setText("用户名不能为空");
passFlag=false;
}
if (pswd.length() == 0){
landUI.getPasswdLabel().setText("密码不能为空");
passFlag=false;
}
if (certStr.length() == 0){
landUI.getStatLabel().setText("验证码不能为空");
passFlag=false;
}
if(passFlag){
if (certStr.equalsIgnoreCase(landUI.getCertPicStrValue())){
this.name=name;
this.pswd=security.getSecurityResult();
landUI.getStatLabel().setText("正在登录...");
start();
} else {
landUI.getStatLabel().setText("验证码错误");
}
} else {
landUI.refreshCertPicLabel();
}
}
public void setIP(String ip){
this.IP=ip;
}
public void setSplitStr(String splitStr){
this.SPLITSTR=splitStr;
}
public void setServerPort(int port){
this.SERVERPORT=port;
}
public void start() { // 建立联网线程
new Thread(new Runnable() {
public void run() {
Socket s = null;
try {
s = new Socket(IP, SERVERPORT);
os = s.getOutputStream();
os.write((name + SPLITSTR + pswd + SPLITSTR + respondStyle).getBytes());
os.flush();
// 读内容
Thread.sleep(1000);
is = s.getInputStream();
int len = is.available();
byte[] bytes = new byte[len];
is.read(bytes);
String result = new String(bytes);
// TODO 这里通过返回结果处理
int index=result.indexOf(SPLITSTR);
String resultStatus=result.substring(0, index);
String resultInfo=result.substring(index+SPLITSTR.length());
if (ISACK.equals(resultStatus)){
landUI.getStatLabel().setText("Welcome:" + resultInfo);
} else if(ISNAK.equals(resultStatus)){ //验证成功后输出对应用户名
landUI.getPasswdField().setText(null);
landUI.getStatLabel().setText("ERROR:" + resultInfo);
} else {
landUI.getStatLabel().setText("ERROR:can not get respond from server");
}
} catch(ConnectException e){
landUI.getStatLabel().setText("ERROR:connect server failed");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
if (os != null) os.close();
if (is != null) is.close();
if (s != null) s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
响应动作之“注册”
package com.net.action;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import com.net.convert.Security;
import com.net.ui.LandWindow;
public class Register implements ActionListener{
private LandWindow landUI=null;
private String name;
private String pswd;
private String IP=null;
private int SERVERPORT=-1;
private String SPLITSTR=null;
private OutputStream os=null;
private InputStream is=null;
private final String respondStyle="register";
private final String ISACK = "ACK";
private final String ISNAK = "NAK!";
public Register(LandWindow landUI) {
this.landUI=landUI;
}
@Override
public void actionPerformed(ActionEvent arg0) {
String name = landUI.getUserNameField().getText();
String pswd = new String(landUI.getPasswdField().getPassword());
String certStr = landUI.getCertTextField().getText();
Security security = new Security(pswd);
security.securityAlgorithm("MD5");
boolean passFlag=true;
landUI.getUserNameLabel().setText(null);
landUI.getPasswdLabel().setText(null);
landUI.getStatLabel().setText(null);
if (name.length() == 0){
landUI.getUserNameLabel().setText("用户名不能为空");
passFlag=false;
}
if (pswd.length() == 0){
landUI.getPasswdLabel().setText("密码不能为空");
passFlag=false;
}
if (certStr.length() == 0){
landUI.getStatLabel().setText("验证码不能为空");
passFlag=false;
}
if(passFlag){
if (certStr.equalsIgnoreCase(landUI.getCertPicStrValue())){
this.name=name;
this.pswd=security.getSecurityResult();
landUI.getStatLabel().setText("提交信息中...");
start();
} else {
landUI.getStatLabel().setText("验证码错误");
}
} else {
landUI.refreshCertPicLabel();
}
}
public void setIP(String ip){
this.IP=ip;
}
public void setSplitStr(String splitStr){
this.SPLITSTR=splitStr;
}
public void setServerPort(int port){
this.SERVERPORT=port;
}
public void start() { // 建立联网线程
new Thread(new Runnable() {
public void run() {
Socket s = null;
try {
s = new Socket(IP, SERVERPORT);
os = s.getOutputStream();
os.write((name + SPLITSTR + pswd + SPLITSTR + respondStyle).getBytes());
os.flush();
// 读内容
Thread.sleep(1000);
is = s.getInputStream();
int len = is.available();
byte[] bytes = new byte[len];
is.read(bytes);
String result = new String(bytes);
// TODO 这里通过返回结果处理
int index=result.indexOf(SPLITSTR);
String resultStatus=result.substring(0, index);
String resultInfo=result.substring(index+SPLITSTR.length());
if (ISACK.equals(resultStatus)){
landUI.getPasswdField().setText(null);
landUI.getStatLabel().setText("INFO:" + resultInfo + " regist successfully");
} else if(ISNAK.equals(resultStatus)){ //验证成功后输出对应用户名
landUI.getPasswdField().setText(null);
landUI.getStatLabel().setText("ERROR:" + resultInfo);
} else {
landUI.getStatLabel().setText("ERROR:can not get respond from server");
}
} catch(ConnectException e){
landUI.getStatLabel().setText("无法连接服务端");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
if (os != null) os.close();
if (is != null) is.close();
if (s != null) s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
响应动作之“重置”
package com.net.action;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.net.ui.LandWindow;
public class Cancle implements ActionListener {
private LandWindow landUI=null;
public Cancle(LandWindow landUI) {
this.landUI=landUI;
//this.landUI.refreshCertPicLabel();
}
private void clearPanel() {
this.landUI.getUserNameField().setText(null);
this.landUI.getUserNameLabel().setText(null);
this.landUI.getPasswdField().setText(null);
this.landUI.getPasswdLabel().setText(null);
this.landUI.getCertTextField().setText(null);
this.landUI.getStatLabel().setText(null);
}
@Override
public void actionPerformed(ActionEvent ae) {
this.clearPanel();
this.landUI.refreshCertPicLabel();
}
}