import java.awt.*; import java.awt.event.*; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.*; import java.sql.*; public class Client extends Frame { List listMsg = new List(); //消息显示列表 Panel panelDown = new Panel(); Panel panelUp = new Panel(); Panel panelChange = new Panel(); TextField tfMsg = new TextField(40); //消息输入框 Button btSend = new Button("发送消息"); // 消息发送按钮 Button btClrMsg = new Button("清除列表"); Button btLogin = new Button("登录"); Label lUsername = new Label("用户名:"); Label lPassword = new Label(" 密码:"); TextField tfUsername = new TextField(15); TextField tfPassword = new TextField(15); Socket s; DataOutputStream dos; DataInputStream dis; String nickname = "匿名"; public Client(String title){ super(title); //界面步局 添加组件 panelUp.add(lUsername); panelUp.add(tfUsername); panelUp.add(lPassword); panelUp.add(tfPassword); tfPassword.setEchoChar('*'); //密码框效果 panelUp.add(btLogin); panelDown.add(tfMsg); panelDown.add(btSend); panelDown.add(btClrMsg); this.add(panelUp,BorderLayout.NORTH); this.add(listMsg,BorderLayout.CENTER); this.add(panelDown,BorderLayout.SOUTH); this.setSize(480, 400); this.setLocation(300, 200); this.setVisible(true); //添加关闭按扭事件监听 this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); SendMonitor sm = new SendMonitor(); //创建一个消息监听器 btSend.addActionListener(sm); //在按钮上注册消息监听 tfMsg.addActionListener(sm); //在文本框上注册消息监听 ClrMsgMonitor cmm = new ClrMsgMonitor(); btClrMsg.addActionListener(cmm); LoginMonitor lm = new LoginMonitor(); btLogin.addActionListener(lm); tfPassword.addActionListener(lm); connectServer(); new Thread(new ReceiveThread()).start(); //创建一个接收的线程并启用 } public static void main(String[] args) { new Client("聊天室1.0"); } public void connectServer(){ try { s = new Socket("127.0.0.1",6666); dos = new DataOutputStream(s.getOutputStream()); dis = new DataInputStream(s.getInputStream()); } catch (UnknownHostException e) { System.out.println("连接服务器失败 UnknownHost异常"); } catch (IOException e) { System.out.println("连接服务器失败,请在服务器打开后尝试"); System.exit(0); } } class SendMonitor implements ActionListener{ //发送 监视器 public void actionPerformed(ActionEvent e) { //实现 String msg = tfMsg.getText(); // 在消息框中获取文字 try { dos.writeUTF(nickname+":"+msg); } catch (IOException e1) { System.out.println("消息写入失败 IOException"); } tfMsg.setText(""); //清空 } } class ClrMsgMonitor implements ActionListener{ //清除列表 监视器 public void actionPerformed(ActionEvent e) { //实现 listMsg.removeAll(); //清楚消息显示列表 } } class ReceiveThread implements Runnable{ //实现ReceiveThread线程 public void run() { while(true){ String msg = null; try { msg = dis.readUTF(); //读输入流 } catch (IOException e) { System.out.println("输入流异常 IOException"); } listMsg.add(msg); //显示 } } } class LoginMonitor implements ActionListener{ //登录按钮 监听器 public void actionPerformed(ActionEvent e) { //实现 Connection cn = null; boolean loginSuccess = false; String userName = tfUsername.getText(); String passWord = tfPassword.getText(); //连接数据库 try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (ClassNotFoundException e1) { System.out.println("数据库连接失败"); } try { cn = DriverManager.getConnection("jdbc:odbc:"+"chatUser", "sa",""); } catch (SQLException e1) { System.out.println("数据库连接失败"); } //对数据库进行操作: Statement stmt = null; try { stmt = cn.createStatement(); } catch (SQLException e1) { System.out.println("创建Statement失败"); } ResultSet rs = null; try { rs = stmt.executeQuery("SELECT * FROM chatUser"); } catch (SQLException e1) { System.out.println("查询失败"); } try { while (rs.next()){ /* test the mdb data for (int i=1;i<=4;i++) { System.out.print(rs.getString(i) + " "); } System.out.println(""); */ if (userName.equals(rs.getString(2)) && passWord.equals(rs.getString(4))) { nickname = rs.getString(3); loginSuccess = true; } } } catch (SQLException e1) { System.out.println("RS操作失败"); } if (loginSuccess) { //如果登陆成功 Label welcome = new Label("welcome "+nickname +" to our ChatRoom!"); remove(panelUp); //删除原panelUp panelChange.add(welcome); //更换新PANEL add(panelChange,BorderLayout.NORTH); listMsg.add("系统消息:登录成功!"); setVisible(false); setVisible(true); } else { //如果登陆失败 tfPassword.setText(""); listMsg.add("系统消息:用户用或密码不正确!请确认后重新输入。"); } } } }