Java高级课程设计--网络聊天室

时间:2014-09-05 05:45:57
【文件属性】:

文件名称:Java高级课程设计--网络聊天室

文件大小:12KB

文件格式:RAR

更新时间:2014-09-05 05:45:57

网络聊天室

自己做的TEST网络聊天 package com.tsing.chat.frame; import java.awt.*; import java.awt.event.*; import java.util.EventListener; import java.io.*; import java.net.*; import javax.swing.JOptionPane; public class ClientFrame extends Frame implements ActionListener { //窗体应该有发送按钮、文本框用来输入东西、List列表用来显示信息 Button connectbutton = new Button("连接"); Button sendbutton = new Button("发送"); List list = new List(); TextField content = new TextField(20); TextField servername = new TextField("localhost", 20); Label label = new Label("输入服务器名字:"); Socket socket; String num; public ClientFrame(String num) { this.num = num; this.setTitle("山寨QQ (用户:" + num + ")"); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try { MsgBean bean = new MsgBean(); bean.setType("请求下线"); bean.setSource(ClientFrame.this.num); //发送消息请求下线 OutputStream out = socket.getOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream( out); objectOutputStream.writeObject(bean); objectOutputStream.flush(); } catch (Exception ew) { ew.printStackTrace(); } //退出系统 System.exit(0); } }); sendbutton.addActionListener(this); connectbutton.addActionListener(this); Panel p1 = new Panel(); p1.add(label); p1.add(servername); p1.add(connectbutton); Panel p2 = new Panel(); p2.add(content); p2.add(sendbutton); Panel p3 = new Panel(); p3.setLayout(new BorderLayout()); p3.add("South", p2); list.setFont(new Font("微软雅黑", Font.BOLD, 14)); p3.add(new ScrollPane().add(list)); this.add("South", p1); this.add(p3); this.setSize(400, 300);// this.setResizable(false); //不能缩放窗口 this.setVisible(true); } public static void main(String[] args) { String num = JOptionPane.showInputDialog(null, "输入自己的QQ号码"); boolean b = false; do { b = false; if (num == null || num.equals("")) { num = JOptionPane.showInputDialog("QQ号码不能为空,请重新输入:"); b = true; } } while (b); new ClientFrame(num); } /** * 客户端:发消息给每个人 * 1、启动聊天界面 * 2、输入QQ号和服务器IP(机器名),建立连接 * 3、启动客户端线程,监视服务器是否转发消息,然后显示到界面 * 4、直接通过自己的socket发送消息 * */ public void actionPerformed(ActionEvent e) { if (e.getSource() == connectbutton) { String string = servername.getText(); try { socket = new Socket(string, 9000); connectbutton.setEnabled(false); MsgBean bean = new MsgBean(); bean.setType("请求登陆"); bean.setSource(num); bean.setTarget("admin"); // //发送请求登陆的消息 OutputStream outputStream = socket.getOutputStream(); ObjectOutputStream objout = new ObjectOutputStream(outputStream); objout.writeObject(bean); objout.flush(); // //启动客户端线程,监视服务器转来的消息 new Thread(new ClientThread(this)).start(); JOptionPane.showMessageDialog(null, "连接成功,聊吧!"); } catch (Exception e1) { e1.printStackTrace(); } } else if (e.getSource() == sendbutton) { try { if (socket == null) { JOptionPane.showMessageDialog(this, "请点击链接按钮,连接到服务器!!"); return; } String info = content.getText(); if (info.equals("")) { JOptionPane.showMessageDialog(this, "请输入消息内容!"); return; } MsgBean bean = new MsgBean(); bean.setType("请求聊天"); bean.setSource(num); bean.setInfo(info); OutputStream out = socket.getOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream( out); objectOutputStream.writeObject(bean); objectOutputStream.flush(); content.setText(""); } catch (IOException e1) { e1.printStackTrace(); } } } }


【文件预览】:
chat
----.project(380B)
----bin()
--------com()
----src()
--------com()
----.classpath(232B)

网友评论