主要代码如下:
public void actionPerformed(ActionEvent arg0) {
if(arg0.getActionCommand().equals("登陆")){
try {
// 发送用户名和密码到服务器
String user = txtUser.getText();
String pass = txtPass.getText();
Socket s = new Socket("127.0.0.1" , 8000) ;
OutputStream os = s.getOutputStream() ;
OutputStreamWriter osw = new OutputStreamWriter(os) ;
PrintWriter pw = new PrintWriter(osw , true) ;
pw.println(user+"%"+pass) ;
//接受服务器发送回来的确认信息
InputStream is = s.getInputStream() ;
InputStreamReader isr = new InputStreamReader(is) ;
BufferedReader br = new BufferedReader(isr) ;
String yorn = br.readLine() ;
//显示主窗体
if (yorn.equals("ok")) {
QQMain w = new QQMain();
w.setVisible(true);
this.setVisible(false);
}else {
JOptionPane.showMessageDialog(this, "对不起,用户名或密码错误") ;
}
} catch (Exception e) {}
}
if(arg0.getActionCommand().equals("注册")){
System.out.println("用户点了注册") ;
}
if(arg0.getActionCommand().equals("取消")){
System.out.println("用户点了取消") ;
}
}
|
|