因为代码的重用性,所以采用单实例的模式:
抽象接口(父类)ActionInterface:
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 abstract class ActionInterface implements ActionListener {
protected LandWindow landUI=null;
protected String name;
protected String pswd;
protected String IP=null;
protected int SERVERPORT=-1;
protected String SPLITSTR=null;
protected OutputStream os=null;
protected InputStream is=null;
protected String respondStyle="style";
protected final String ISACK = "ACK";
protected final String ISNAK = "NAK!";
public ActionInterface(LandWindow landUI) {
this.landUI=landUI;
}
public void actionPerformed(ActionEvent arg0) {
landUI.getUserNameLabel().setText(null);
landUI.getPasswdLabel().setText(null);
landUI.getStatLabel().setText(null);
}
public void setIP(String ip){
this.IP=ip;
}
public void setSplitStr(String splitStr){
this.SPLITSTR=splitStr;
}
public void setServerPort(int port){
this.SERVERPORT=port;
}
protected 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 这里通过返回结果处理
String[] strs=result.split(SPLITSTR);
String resultStatus=strs[0];
String resultInfo=strs[1];
if (ISACK.equals(resultStatus)){
landUI.getPasswdField().setText(null);
landUI.getStatLabel().setText("INFO:" + resultInfo + respondStyle + " 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("ERROR:Can not connect server");
} 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();
}
protected String getSecurityString(String str){
Security security = new Security(str);
security.securityAlgorithm("MD5");
return security.getSecurityResult();
}
}
// 响应之“重置” package com.net.action; import java.awt.event.ActionEvent; import com.net.ui.LandWindow; public class Cancle extends ActionInterface { public Cancle(LandWindow landUI) { super(landUI); } private void clearPanel() { this.landUI.getUserNameField().setText(null); this.landUI.getPasswdField().setText(null); this.landUI.getCertTextField().setText(null); } @Override public void actionPerformed(ActionEvent ae) { super.actionPerformed(ae); this.clearPanel(); this.landUI.refreshCertPicLabel(); } }
//响应之“登录” package com.net.action; import java.awt.event.ActionEvent; import com.net.ui.LandWindow; public class Submit extends ActionInterface{ public Submit(LandWindow landUI) { super(landUI); this.respondStyle="land"; this.landUI.refreshCertPicLabel(); } @Override public void actionPerformed(ActionEvent arg0) { super.actionPerformed(arg0); String name = landUI.getUserNameField().getText(); String pswd = new String(landUI.getPasswdField().getPassword()); String certStr = landUI.getCertTextField().getText(); boolean passFlag=true; 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=getSecurityString(pswd); landUI.getStatLabel().setText("正在登录..."); this.start(); } else { landUI.getStatLabel().setText("验证码错误"); } } else { landUI.refreshCertPicLabel(); } } }
//响应之“注册”
package com.net.action; import java.awt.event.ActionEvent; import com.net.ui.LandWindow; public class Register extends ActionInterface{ public Register(LandWindow landUI) { super(landUI); this.respondStyle="register"; } @Override public void actionPerformed(ActionEvent arg0) { super.actionPerformed(arg0); String name = landUI.getUserNameField().getText(); String pswd = new String(landUI.getPasswdField().getPassword()); String certStr = landUI.getCertTextField().getText(); boolean passFlag=true; 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=getSecurityString(pswd); landUI.getStatLabel().setText("提交信息中..."); this.start(); } else { landUI.getStatLabel().setText("验证码错误"); } } else { landUI.refreshCertPicLabel(); } } }