import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.TextField;import javax.swing.JFrame;
import javax.swing.JPanel;public class ClientFrame extends JFrame { private static final long serialVersionUID = 1L; private JPanel jp1 = new JPanel();
private TextField userName = new TextField(20);
private TextField password = new TextField(20);
private Button logButton = new Button("登陆");
public void Jp1Init(){
jp1.setLayout(new FlowLayout());
jp1.add(userName);
jp1.add(password);
password.setEchoChar('*');
jp1.add(logButton);
logButton.addActionListener(new LoginActionListener(userName,password,this));
}
public ClientFrame(){
this.jframeInit();
}
public void jframeInit(){
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.Jp1Init();
this.add(jp1);
this.setVisible(true);
}
public static void main(String[] args) {
new ClientFrame();
}
}
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;public class LoginActionListener implements ActionListener { private Socket clientSocket;
private ClientFrame clientFrame;
private TextField userName;
private TextField password;
public LoginActionListener(TextField userName,TextField password,ClientFrame clientFrame){
try {
this.clientSocket = new Socket(InetAddress.getByName("192.168.18.4"),7999);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this.userName = userName;
this.password = password;
this.clientFrame = clientFrame;
}
public void actionPerformed(ActionEvent event) {
String name = userName.getText();
String mima = password.getText();
if("".equals(name) && "".equals(mima)){
//这块不用看,不输入东西的时候没执行,直接看else里
System.out.println("账号密码输入格式正确");
OutputStream os = null;
//向服务器发送账号密码
try {
os = clientSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
try {
dos.writeUTF("username" + name + "#^$!$##password" + mima);
} catch (IOException e) {
e.printStackTrace();
}
//返回是否登陆成功
InputStream is = null;
try {
is = clientSocket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);
String logMark = null;
try {
logMark = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
}
//登陆成功
if(logMark == "true"){
try {
dis.close();
bis.close();
is.close();
dos.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
clientFrame.removeAll();
new LoginFrame(clientFrame,clientSocket);
}
//登陆失败
if(logMark == "false"){
System.out.println("账号和密码错误");
try {
dis.close();
bis.close();
is.close();
dos.close();
bos.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}else{
System.out.println("账号密码格式不正确");
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}当我什么都不输入的时候,点击登陆按钮
打印出:
账号密码格式不正确
然后就卡住了,不能在文本框输入东西,也不能点击登陆按钮
为什么?
如果关闭的话,那下次再点登录,不是就抛空了么?我觉得,就是client端强行关闭连接的时候出问题了。你把try那段注掉,直接return看看。
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
这段去掉后还是一样
//这块不用看,不输入东西的时候没执行,直接看else里
System.out.println("账号密码输入格式正确");
你确定这里没执行吗?什么都没输入的时候name和mima不是空?你把打印的语句改了,不要和else里一样。如果是这里打印的。那就是执行了。而ServerSocket返回的值不是true也不是false的话,那就会像你说的一样:卡住了。
if 和 else 里的内容刚好弄反了